nsm.el 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. ;;; nsm.el --- Network Security Manager
  2. ;; Copyright (C) 2014-2017 Free Software Foundation, Inc.
  3. ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
  4. ;; Keywords: encryption, security, network
  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. ;;; Code:
  18. (require 'cl-lib)
  19. (require 'subr-x) ; read-multiple-choice
  20. (defvar nsm-permanent-host-settings nil)
  21. (defvar nsm-temporary-host-settings nil)
  22. (defgroup nsm nil
  23. "Network Security Manager"
  24. :version "25.1"
  25. :group 'comm)
  26. (defcustom network-security-level 'medium
  27. "How secure the network should be.
  28. If a potential problem with the security of the network
  29. connection is found, the user is asked to give input into how the
  30. connection should be handled.
  31. The following values are possible:
  32. `low': Absolutely no checks are performed.
  33. `medium': This is the default level, should be reasonable for most usage.
  34. `high': This warns about additional things that many people would
  35. not find useful.
  36. `paranoid': On this level, the user is queried for most new connections.
  37. See the Emacs manual for a description of all things that are
  38. checked and warned against."
  39. :version "25.1"
  40. :group 'nsm
  41. :type '(choice (const :tag "Low" low)
  42. (const :tag "Medium" medium)
  43. (const :tag "High" high)
  44. (const :tag "Paranoid" paranoid)))
  45. (defcustom nsm-settings-file (expand-file-name "network-security.data"
  46. user-emacs-directory)
  47. "The file the security manager settings will be stored in."
  48. :version "25.1"
  49. :group 'nsm
  50. :type 'file)
  51. (defcustom nsm-save-host-names nil
  52. "If non-nil, always save host names in the structures in `nsm-settings-file'.
  53. By default, only hosts that have exceptions have their names
  54. stored in plain text."
  55. :version "25.1"
  56. :group 'nsm
  57. :type 'boolean)
  58. (defvar nsm-noninteractive nil
  59. "If non-nil, the connection is opened in a non-interactive context.
  60. This means that no queries should be performed.")
  61. (declare-function gnutls-peer-status "gnutls.c" (proc))
  62. (defun nsm-verify-connection (process host port &optional
  63. save-fingerprint warn-unencrypted)
  64. "Verify the security status of PROCESS that's connected to HOST:PORT.
  65. If PROCESS is a gnutls connection, the certificate validity will
  66. be examined. If it's a non-TLS connection, it may be compared
  67. against previous connections. If the function determines that
  68. there is something odd about the connection, the user will be
  69. queried about what to do about it.
  70. The process is returned if everything is OK, and otherwise, the
  71. process will be deleted and nil is returned.
  72. If SAVE-FINGERPRINT, always save the fingerprint of the
  73. server (if the connection is a TLS connection). This is useful
  74. to keep track of the TLS status of STARTTLS servers.
  75. If WARN-UNENCRYPTED, query the user if the connection is
  76. unencrypted."
  77. (if (eq network-security-level 'low)
  78. process
  79. (let* ((status (gnutls-peer-status process))
  80. (id (nsm-id host port))
  81. (settings (nsm-host-settings id)))
  82. (cond
  83. ((not (process-live-p process))
  84. nil)
  85. ((not status)
  86. ;; This is a non-TLS connection.
  87. (nsm-check-plain-connection process host port settings
  88. warn-unencrypted))
  89. (t
  90. (let ((process
  91. (nsm-check-tls-connection process host port status settings)))
  92. (when (and process save-fingerprint
  93. (null (nsm-host-settings id)))
  94. (nsm-save-host host port status 'fingerprint 'always))
  95. process))))))
  96. (defun nsm-check-tls-connection (process host port status settings)
  97. (let ((process (nsm-check-certificate process host port status settings)))
  98. (if (and process
  99. (>= (nsm-level network-security-level) (nsm-level 'high)))
  100. ;; Do further protocol-level checks if the security is high.
  101. (nsm-check-protocol process host port status settings)
  102. process)))
  103. (declare-function gnutls-peer-status-warning-describe "gnutls.c"
  104. (status-symbol))
  105. (defun nsm-check-certificate (process host port status settings)
  106. (let ((warnings (plist-get status :warnings)))
  107. (cond
  108. ;; The certificate validated, but perhaps we want to do
  109. ;; certificate pinning.
  110. ((null warnings)
  111. (cond
  112. ((< (nsm-level network-security-level) (nsm-level 'high))
  113. process)
  114. ;; The certificate is fine, but if we're paranoid, we might
  115. ;; want to check whether it's changed anyway.
  116. ((and (>= (nsm-level network-security-level) (nsm-level 'high))
  117. (not (nsm-fingerprint-ok-p host port status settings)))
  118. (delete-process process)
  119. nil)
  120. ;; We haven't seen this before, and we're paranoid.
  121. ((and (eq network-security-level 'paranoid)
  122. (null settings)
  123. (not (nsm-new-fingerprint-ok-p host port status)))
  124. (delete-process process)
  125. nil)
  126. ((>= (nsm-level network-security-level) (nsm-level 'high))
  127. ;; Save the host fingerprint so that we can check it the
  128. ;; next time we connect.
  129. (nsm-save-host host port status 'fingerprint 'always)
  130. process)
  131. (t
  132. process)))
  133. ;; The certificate did not validate.
  134. ((not (equal network-security-level 'low))
  135. ;; We always want to pin the certificate of invalid connections
  136. ;; to track man-in-the-middle or the like.
  137. (if (not (nsm-fingerprint-ok-p host port status settings))
  138. (progn
  139. (delete-process process)
  140. nil)
  141. ;; We have a warning, so query the user.
  142. (if (and (not (nsm-warnings-ok-p status settings))
  143. (not (nsm-query
  144. host port status 'conditions
  145. "The TLS connection to %s:%s is insecure for the following reason%s:\n\n%s"
  146. host port
  147. (if (> (length warnings) 1)
  148. "s" "")
  149. (mapconcat #'gnutls-peer-status-warning-describe
  150. warnings
  151. "\n"))))
  152. (progn
  153. (delete-process process)
  154. nil)
  155. process))))))
  156. (defun nsm-check-protocol (process host port status settings)
  157. (let ((prime-bits (plist-get status :diffie-hellman-prime-bits))
  158. (signature-algorithm
  159. (plist-get (plist-get status :certificate) :signature-algorithm))
  160. (encryption (format "%s-%s-%s"
  161. (plist-get status :key-exchange)
  162. (plist-get status :cipher)
  163. (plist-get status :mac)))
  164. (protocol (plist-get status :protocol)))
  165. (cond
  166. ((and prime-bits
  167. (< prime-bits 1024)
  168. (not (memq :diffie-hellman-prime-bits
  169. (plist-get settings :conditions)))
  170. (not
  171. (nsm-query
  172. host port status :diffie-hellman-prime-bits
  173. "The Diffie-Hellman prime bits (%s) used for this connection to %s:%s is less than what is considered safe (%s)."
  174. prime-bits host port 1024)))
  175. (delete-process process)
  176. nil)
  177. ((and (string-match "\\bRC4\\b" encryption)
  178. (not (memq :rc4 (plist-get settings :conditions)))
  179. (not
  180. (nsm-query
  181. host port status :rc4
  182. "The connection to %s:%s uses the RC4 algorithm (%s), which is believed to be unsafe."
  183. host port encryption)))
  184. (delete-process process)
  185. nil)
  186. ((and (string-match "\\bSHA1\\b" signature-algorithm)
  187. (not (memq :signature-sha1 (plist-get settings :conditions)))
  188. (not
  189. (nsm-query
  190. host port status :signature-sha1
  191. "The certificate used to verify the connection to %s:%s uses the SHA1 algorithm (%s), which is believed to be unsafe."
  192. host port signature-algorithm)))
  193. (delete-process process)
  194. nil)
  195. ((and protocol
  196. (string-match "SSL" protocol)
  197. (not (memq :ssl (plist-get settings :conditions)))
  198. (not
  199. (nsm-query
  200. host port status :ssl
  201. "The connection to %s:%s uses the %s protocol, which is believed to be unsafe."
  202. host port protocol)))
  203. (delete-process process)
  204. nil)
  205. (t
  206. process))))
  207. (defun nsm-fingerprint (status)
  208. (plist-get (plist-get status :certificate) :public-key-id))
  209. (defun nsm-fingerprint-ok-p (host port status settings)
  210. (let ((did-query nil))
  211. (if (and settings
  212. (not (eq (plist-get settings :fingerprint) :none))
  213. (not (equal (nsm-fingerprint status)
  214. (plist-get settings :fingerprint)))
  215. (not
  216. (setq did-query
  217. (nsm-query
  218. host port status 'fingerprint
  219. "The fingerprint for the connection to %s:%s has changed from %s to %s"
  220. host port
  221. (plist-get settings :fingerprint)
  222. (nsm-fingerprint status)))))
  223. ;; Not OK.
  224. nil
  225. (when did-query
  226. ;; Remove any exceptions that have been set on the previous
  227. ;; certificate.
  228. (plist-put settings :conditions nil))
  229. t)))
  230. (defun nsm-new-fingerprint-ok-p (host port status)
  231. (nsm-query
  232. host port status 'fingerprint
  233. "The fingerprint for the connection to %s:%s is new: %s"
  234. host port
  235. (nsm-fingerprint status)))
  236. (defun nsm-check-plain-connection (process host port settings warn-unencrypted)
  237. ;; If this connection used to be TLS, but is now plain, then it's
  238. ;; possible that we're being Man-In-The-Middled by a proxy that's
  239. ;; stripping out STARTTLS announcements.
  240. (cond
  241. ((and (plist-get settings :fingerprint)
  242. (not (eq (plist-get settings :fingerprint) :none))
  243. (not
  244. (nsm-query
  245. host port nil 'conditions
  246. "The connection to %s:%s used to be an encrypted connection, but is now unencrypted. This might mean that there's a man-in-the-middle tapping this connection."
  247. host port)))
  248. (delete-process process)
  249. nil)
  250. ((and warn-unencrypted
  251. (not (memq :unencrypted (plist-get settings :conditions)))
  252. (not (nsm-query
  253. host port nil 'conditions
  254. "The connection to %s:%s is unencrypted."
  255. host port)))
  256. (delete-process process)
  257. nil)
  258. (t
  259. process)))
  260. (defun nsm-query (host port status what message &rest args)
  261. ;; If there is no user to answer queries, then say `no' to everything.
  262. (if (or noninteractive
  263. nsm-noninteractive)
  264. nil
  265. (let ((response
  266. (condition-case nil
  267. (intern
  268. (car (split-string
  269. (nsm-query-user message args
  270. (nsm-format-certificate status))))
  271. obarray)
  272. ;; Make sure we manage to close the process if the user hits
  273. ;; `C-g'.
  274. (quit 'no)
  275. (error 'no))))
  276. (if (eq response 'no)
  277. (progn
  278. (message "Aborting connection to %s:%s" host port)
  279. nil)
  280. (message (if (eq response 'session)
  281. "Accepting certificate for %s:%s this session only"
  282. "Permanently accepting certificate for %s:%s")
  283. host port)
  284. (nsm-save-host host port status what response)
  285. t))))
  286. (defun nsm-query-user (message args cert)
  287. (let ((buffer (get-buffer-create "*Network Security Manager*")))
  288. (save-window-excursion
  289. ;; First format the certificate and warnings.
  290. (with-help-window buffer
  291. (with-current-buffer buffer
  292. (erase-buffer)
  293. (when (> (length cert) 0)
  294. (insert cert "\n"))
  295. (let ((start (point)))
  296. (insert (apply #'format-message message args))
  297. (goto-char start)
  298. ;; Fill the first line of the message, which usually
  299. ;; contains lots of explanatory text.
  300. (fill-region (point) (line-end-position)))))
  301. ;; Then ask the user what to do about it.
  302. (unwind-protect
  303. (cadr
  304. (read-multiple-choice
  305. "Continue connecting?"
  306. '((?a "always" "Accept this certificate this session and for all future sessions.")
  307. (?s "session only" "Accept this certificate this session only.")
  308. (?n "no" "Refuse to use this certificate, and close the connection."))))
  309. (kill-buffer buffer)))))
  310. (defun nsm-save-host (host port status what permanency)
  311. (let* ((id (nsm-id host port))
  312. (saved
  313. (list :id id
  314. :fingerprint (or (nsm-fingerprint status)
  315. ;; Plain connection.
  316. :none))))
  317. (when (or (eq what 'conditions)
  318. nsm-save-host-names)
  319. (nconc saved (list :host (format "%s:%s" host port))))
  320. ;; We either want to save/update the fingerprint or the conditions
  321. ;; of the certificate/unencrypted connection.
  322. (cond
  323. ((eq what 'conditions)
  324. (cond
  325. ((not status)
  326. (nconc saved '(:conditions (:unencrypted))))
  327. ((plist-get status :warnings)
  328. (nconc saved
  329. (list :conditions (plist-get status :warnings))))))
  330. ((not (eq what 'fingerprint))
  331. ;; Store additional protocol settings.
  332. (let ((settings (nsm-host-settings id)))
  333. (when settings
  334. (setq saved settings))
  335. (if (plist-get saved :conditions)
  336. (nconc (plist-get saved :conditions) (list what))
  337. (nconc saved (list :conditions (list what)))))))
  338. (if (eq permanency 'always)
  339. (progn
  340. (nsm-remove-temporary-setting id)
  341. (nsm-remove-permanent-setting id)
  342. (push saved nsm-permanent-host-settings)
  343. (nsm-write-settings))
  344. (nsm-remove-temporary-setting id)
  345. (push saved nsm-temporary-host-settings))))
  346. (defun nsm-write-settings ()
  347. (with-temp-file nsm-settings-file
  348. (insert "(\n")
  349. (dolist (setting nsm-permanent-host-settings)
  350. (insert " ")
  351. (prin1 setting (current-buffer))
  352. (insert "\n"))
  353. (insert ")\n")))
  354. (defun nsm-read-settings ()
  355. (setq nsm-permanent-host-settings
  356. (with-temp-buffer
  357. (insert-file-contents nsm-settings-file)
  358. (goto-char (point-min))
  359. (ignore-errors (read (current-buffer))))))
  360. (defun nsm-id (host port)
  361. (concat "sha1:" (sha1 (format "%s:%s" host port))))
  362. (defun nsm-host-settings (id)
  363. (when (and (not nsm-permanent-host-settings)
  364. (file-exists-p nsm-settings-file))
  365. (nsm-read-settings))
  366. (let ((result nil))
  367. (dolist (elem (append nsm-temporary-host-settings
  368. nsm-permanent-host-settings))
  369. (when (and (not result)
  370. (equal (plist-get elem :id) id))
  371. (setq result elem)))
  372. result))
  373. (defun nsm-warnings-ok-p (status settings)
  374. (let ((ok t)
  375. (conditions (plist-get settings :conditions)))
  376. (dolist (warning (plist-get status :warnings))
  377. (unless (memq warning conditions)
  378. (setq ok nil)))
  379. ok))
  380. (defun nsm-remove-permanent-setting (id)
  381. (setq nsm-permanent-host-settings
  382. (cl-delete-if
  383. (lambda (elem)
  384. (equal (plist-get elem :id) id))
  385. nsm-permanent-host-settings)))
  386. (defun nsm-remove-temporary-setting (id)
  387. (setq nsm-temporary-host-settings
  388. (cl-delete-if
  389. (lambda (elem)
  390. (equal (plist-get elem :id) id))
  391. nsm-temporary-host-settings)))
  392. (defun nsm-format-certificate (status)
  393. (let ((cert (plist-get status :certificate)))
  394. (when cert
  395. (with-temp-buffer
  396. (insert
  397. "Certificate information\n"
  398. "Issued by:"
  399. (nsm-certificate-part (plist-get cert :issuer) "CN" t) "\n"
  400. "Issued to:"
  401. (or (nsm-certificate-part (plist-get cert :subject) "O")
  402. (nsm-certificate-part (plist-get cert :subject) "OU" t))
  403. "\n"
  404. "Hostname:"
  405. (nsm-certificate-part (plist-get cert :subject) "CN" t) "\n")
  406. (when (and (plist-get cert :public-key-algorithm)
  407. (plist-get cert :signature-algorithm))
  408. (insert
  409. "Public key:" (plist-get cert :public-key-algorithm)
  410. ", signature: " (plist-get cert :signature-algorithm) "\n"))
  411. (when (and (plist-get status :key-exchange)
  412. (plist-get status :cipher)
  413. (plist-get status :mac)
  414. (plist-get status :protocol))
  415. (insert
  416. "Protocol:" (plist-get status :protocol)
  417. ", key: " (plist-get status :key-exchange)
  418. ", cipher: " (plist-get status :cipher)
  419. ", mac: " (plist-get status :mac) "\n"))
  420. (when (plist-get cert :certificate-security-level)
  421. (insert
  422. "Security level:"
  423. (propertize (plist-get cert :certificate-security-level)
  424. 'face 'bold)
  425. "\n"))
  426. (insert
  427. "Valid:From " (plist-get cert :valid-from)
  428. " to " (plist-get cert :valid-to) "\n\n")
  429. (goto-char (point-min))
  430. (while (re-search-forward "^[^:]+:" nil t)
  431. (insert (make-string (- 20 (current-column)) ? )))
  432. (buffer-string)))))
  433. (defun nsm-certificate-part (string part &optional full)
  434. (let ((part (cadr (assoc part (nsm-parse-subject string)))))
  435. (cond
  436. (part part)
  437. (full string)
  438. (t nil))))
  439. (defun nsm-parse-subject (string)
  440. (with-temp-buffer
  441. (insert string)
  442. (goto-char (point-min))
  443. (let ((start (point))
  444. (result nil))
  445. (while (not (eobp))
  446. (push (replace-regexp-in-string
  447. "[\\]\\(.\\)" "\\1"
  448. (buffer-substring start
  449. (if (re-search-forward "[^\\]," nil 'move)
  450. (1- (point))
  451. (point))))
  452. result)
  453. (setq start (point)))
  454. (mapcar
  455. (lambda (elem)
  456. (let ((pos (cl-position ?= elem)))
  457. (if pos
  458. (list (substring elem 0 pos)
  459. (substring elem (1+ pos)))
  460. elem)))
  461. (nreverse result)))))
  462. (defun nsm-level (symbol)
  463. "Return a numerical level for SYMBOL for easier comparison."
  464. (cond
  465. ((eq symbol 'low) 0)
  466. ((eq symbol 'medium) 1)
  467. ((eq symbol 'high) 2)
  468. (t 3)))
  469. (provide 'nsm)
  470. ;;; nsm.el ends here