| 1 |
;;; mew-ssl.el |
| 2 |
|
| 3 |
;; Author: Kazu Yamamoto <[email protected]> |
| 4 |
;; Created: Jul 25, 2002 |
| 5 |
|
| 6 |
;;; Code: |
| 7 |
|
| 8 |
(require 'mew) |
| 9 |
|
| •10 |
(defun mew-ssl-native-p (type) |
| 11 |
"Return if the type is native or not" |
| 12 |
(or (eq type 'native) |
| 13 |
(and (eq type t) (eq mew-ssl-default 'native)))) |
| 14 |
(defun mew-ssl-starttls-p (type port sslport) |
| 15 |
"Return if STARTTLS should be used or not" |
| 16 |
(and type (mew-port-equal port sslport))) |
| 17 |
(defvar mew-ssl-min-prime-bits 2048 |
| 18 |
"Default prime bits for GnuTLS connection.") |
| 19 |
(defvar mew-ssl-verify-error nil |
| 20 |
"verify-error parameter passed to GnuTLS. You might want to |
| 21 |
keep this as nil.") |
| 22 |
(defvar mew-ssl-native-starttls-plist |
| 23 |
'( |
| 24 |
;; RFC 3207 |
| 25 |
(smtp . (:capability-command |
| 26 |
(format "EHLO %s\r\n" (mew-smtp-helo-domain case)) |
| 27 |
:always-query-capabilities t |
| 28 |
:end-of-command |
| 29 |
(format "^[0-9]+ .*\r?\n") |
| 30 |
:end-of-capability |
| 31 |
(format "^[0-9]+ .*\r?\n") |
| 32 |
:success (format "^2.*\r?\n") |
| 33 |
:starttls-function |
| 34 |
(lambda (capabilities) |
| 35 |
(and (string-match "[ -]STARTTLS" capabilities) |
| 36 |
"STARTTLS\r\n")))) |
| 37 |
;; RFC 2595 |
| 38 |
(imap . (:capability-command |
| 39 |
(format "1 CAPABILITY\r\n") |
| 40 |
:always-query-capabilities t |
| 41 |
:end-of-capability |
| 42 |
(format "\r?\n") |
| 43 |
:end-of-command |
| 44 |
(format "\r?\n") |
| 45 |
:success |
| 46 |
(format "^1 OK ") |
| 47 |
:starttls-function |
| 48 |
(lambda (capabilities) |
| 49 |
(when (string-match-p "STARTTLS" capabilities) |
| 50 |
"1 STARTTLS\r\n")))) |
| 51 |
;; RFC 2595 |
| 52 |
(pop . (:capability-command |
| 53 |
(format "CAPA\r\n") |
| 54 |
:always-query-capabilities nil |
| 55 |
:end-of-capability |
| 56 |
(format "^\\.\r?\n\\|^-ERR") |
| 57 |
:end-of-command |
| 58 |
(format "^\\(-ERR\\|+OK\\).*\r?\n") |
| 59 |
:success |
| 60 |
(format "^\\+OK.*\n") |
| 61 |
:starttls-function |
| 62 |
(lambda (capabilities) |
| 63 |
(and (string-match "\\bSTLS\\b" capabilities) |
| 64 |
"STLS\r\n")))) |
| 65 |
;; RFC 4642 |
| 66 |
(nntp . (:capability-command |
| 67 |
(format "CAPABILITIES\r?\n") |
| 68 |
:always-query-capabilities t |
| 69 |
:end-of-capability |
| 70 |
(format "^\\.\r?\n") |
| 71 |
:end-of-command |
| 72 |
(format "^\\.\r?\n") |
| 73 |
:success |
| 74 |
(format "^2.+ \r?\n") |
| 75 |
:starttls-function |
| 76 |
(lambda (capabilities) |
| 77 |
(when (string-match-p "STARTTLS" capabilities) |
| 78 |
"STARTTLS\r\n")))) |
| 79 |
)) |
| 80 |
(defun mew-starttls-get-param (proto key evalp) |
| 81 |
"Get parameter from mew-ssl-native-starttls-plist" |
| 82 |
(let ((p (plist-get (cdr (assq proto mew-ssl-native-starttls-plist)) key))) |
| 83 |
(if evalp (eval p) p))) |
| 84 |
|
| 85 |
(defvar mew-prog-ssl "stunnel") |
| 86 |
(defvar mew-ssl-cert-directory "~/.certs" |
| 87 |
"The directory where certificates of root servers are stored. |
| 88 |
A file name of a certificate should be 'cert-hash.0'. |
| 89 |
'cert-hash' can be extracted by 'openssl x509 -hash -noout -in cert.pem'.") |
| 90 |
|
| 91 |
(defvar mew-ssl-verify-level 1 |
| 92 |
"Verification level of server's certificate. |
| 93 |
0 - no verification. |
| 94 |
1 - verify server's certificate if present. If verification failed, an |
| 95 |
SSL/TLS connection is not created. If not present, an SSL/TLS connection |
| 96 |
is created. |
| 97 |
2 - verify server's certificate. If verification failed, an SSL/TLS |
| 98 |
connection is not created. If not present, an SSL/TLS connection is |
| 99 |
not created. |
| 100 |
3 - verify server's certificate which locally installed (not one from |
| 101 |
the server).") |
| 102 |
|
| 103 |
(defvar mew-prog-ssl-arg nil |
| 104 |
"For stunnel v3, a list of command-line arguments, each one a string. |
| 105 |
For stunnel v4 or v5, a string of extra text to place in the configuration |
| 106 |
file, which should end with a newline (example: \"fips=no\\n\"); or nil to |
| 107 |
insert no extra text.") |
| 108 |
|
| 109 |
(defvar mew-ssl-ver nil) |
| 110 |
(defvar mew-ssl-minor-ver nil) |
| 111 |
|
| 112 |
(defvar mew-ssl-libwrap nil) |
| 113 |
(defvar mew-ssl-unixlike nil |
| 114 |
"Set to t when stunnel supports \"foreground\" option.") |
| 115 |
(defvar mew-ssl-checkhost nil) |
| 116 |
|
| 117 |
(defconst mew-ssl-process-exec-cnt 3) |
| 118 |
|
| 119 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 120 |
;;; |
| 121 |
;;; Magic words |
| 122 |
;;; |
| 123 |
|
| 124 |
(defconst mew-tls-smtp "smtp") |
| 125 |
(defconst mew-tls-pop "pop3") |
| 126 |
(defconst mew-tls-nntp "nntp") |
| 127 |
(defconst mew-tls-imap "imap") ;; xxx stunnel does not support this. |
| 128 |
|
| 129 |
;; stunnel does not support IPv6, sigh |
| 130 |
(defconst mew-ssl-localhost "127.0.0.1") |
| 131 |
|
| 132 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 133 |
;;; |
| 134 |
;;; SSL/TLS info |
| 135 |
;;; |
| 136 |
|
| 137 |
(defvar mew-ssl-info-list '("status" "try" "file" "string")) |
| 138 |
|
| 139 |
(mew-info-defun "mew-ssl-" mew-ssl-info-list) |
| 140 |
|
| 141 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 142 |
;;; |
| 143 |
;;; Process name |
| 144 |
;;; |
| 145 |
|
| 146 |
(defconst mew-ssl-info-prefix "mew-ssl-info-") |
| 147 |
|
| 148 |
(defun mew-ssl-info-name (server remoteport localport) |
| 149 |
(format "%s:%s:%d:%d" mew-ssl-info-prefix server remoteport localport)) |
| 150 |
|
| 151 |
(defun mew-ssl-info-name-regex (server remoteport) |
| 152 |
(format "^%s:%s:%d" mew-ssl-info-prefix server remoteport)) |
| 153 |
|
| 154 |
(defun mew-ssl-pnm-to-lport (pnm) |
| 155 |
(if (string-match ":\\([0-9]+\\)$" pnm) (match-string 1 pnm))) |
| 156 |
|
| 157 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 158 |
;;; |
| 159 |
;;; Executing SSL/TLS |
| 160 |
;;; |
| 161 |
|
| 162 |
(defun mew-ssl-server (server) |
| 163 |
(if (string= server "localhost") |
| 164 |
mew-ssl-localhost |
| 165 |
server)) |
| 166 |
|
| 167 |
(defun mew-ssl-options (case server remoteport localport tls) |
| 168 |
(setq server (mew-ssl-server server)) |
| 169 |
(if (= mew-ssl-ver 3) |
| 170 |
(let (args) |
| 171 |
(setq args |
| 172 |
`("-c" "-f" |
| 173 |
"-a" ,(expand-file-name (mew-ssl-cert-directory case)) |
| 174 |
"-d" ,(format "%s:%d" mew-ssl-localhost localport) |
| 175 |
"-v" ,(number-to-string (mew-ssl-verify-level case)) |
| 176 |
"-D" "debug" |
| 177 |
"-P" "" |
| 178 |
"-r" ,(format "%s:%d" server remoteport) |
| 179 |
,@mew-prog-ssl-arg)) |
| 180 |
(if tls (setq args (cons "-n" (cons tls args)))) |
| 181 |
args) |
| 182 |
(let ((file (mew-make-temp-name))) |
| 183 |
(with-temp-buffer |
| 184 |
(insert "client=yes\n") |
| 185 |
(if mew-ssl-unixlike |
| 186 |
(insert "pid=\n")) |
| 187 |
(insert (format "verify=%d\n" (mew-ssl-verify-level case))) |
| 188 |
(if (and (> (mew-ssl-verify-level case) 0) mew-ssl-checkhost) |
| 189 |
(insert (format "checkHost=%s\n" server))) |
| 190 |
(if mew-ssl-unixlike |
| 191 |
(insert "foreground=yes\n")) |
| 192 |
(insert "debug=debug\n") |
| 193 |
(if (and mew-ssl-libwrap (or (>= mew-ssl-ver 5) (>= mew-ssl-minor-ver 45))) |
| 194 |
(insert "libwrap=no\n")) |
| 195 |
(if (and (or (>= mew-ssl-ver 5) (>= mew-ssl-minor-ver 22)) |
| 196 |
mew-ssl-unixlike) |
| 197 |
(insert "syslog=no\n")) |
| 198 |
(insert "CApath=" (expand-file-name (mew-ssl-cert-directory case)) "\n") |
| 199 |
(if mew-prog-ssl-arg |
| 200 |
(insert mew-prog-ssl-arg)) |
| 201 |
(insert (format "[%d]\n" localport)) |
| 202 |
(insert (format "accept=%s:%d\n" mew-ssl-localhost localport)) |
| 203 |
(insert (format "connect=%s:%d\n" server remoteport)) |
| 204 |
(if tls (insert (format "protocol=%s\n" tls))) |
| 205 |
(mew-frwlet mew-cs-dummy mew-cs-text-for-write |
| 206 |
;; NEVER use call-process-region for privacy reasons |
| 207 |
(write-region (point-min) (point-max) file nil 'no-msg)) |
| 208 |
(list file))))) |
| 209 |
|
| 210 |
(defun mew-open-ssl-stream (case server serv tls) |
| 211 |
"Open an SSL/TLS stream for SERVER's SERV. |
| 212 |
This function returns a process when an SSL/TLS connection is created |
| 213 |
successfully. |
| 214 |
If TLS is nil, an SSL connection is created. |
| 215 |
If TLS is a magic word for 'stunnel', a TLS connection is created. |
| 216 |
A local port number can be obtained the process name after ':'. " |
| 217 |
(cond |
| 218 |
((or (null mew-ssl-ver) (not (mew-which-exec mew-prog-ssl))) |
| 219 |
(message "'%s' is not found" mew-prog-ssl) |
| 220 |
nil) |
| 221 |
(t |
| 222 |
(let* ((remoteport (mew-serv-to-port serv)) |
| 223 |
(localport (+ 8000 (% (mew-random) 4000))) |
| 224 |
(process-connection-type mew-connection-type2) |
| 225 |
(N mew-ssl-process-exec-cnt) |
| 226 |
(pros (process-list)) |
| 227 |
(regex (mew-ssl-info-name-regex server remoteport)) |
| 228 |
name pnm pro dummy bound opts) |
| 229 |
(catch 'find |
| 230 |
(dolist (pr pros) |
| 231 |
(when (string-match regex (process-name pr)) |
| 232 |
(if (memq (process-status pr) '(run)) |
| 233 |
(setq pro pr) |
| 234 |
(delete-process pr)) |
| 235 |
(throw 'find nil)))) |
| 236 |
(if pro |
| 237 |
pro |
| 238 |
(message "Creating an SSL/TLS connection...") |
| 239 |
(setq pro nil) |
| 240 |
(catch 'loop |
| 241 |
(dotimes (_i N) ;; prevent byte-compile warning |
| 242 |
(setq name (mew-ssl-info-name server remoteport localport)) |
| 243 |
(setq opts (mew-ssl-options case server remoteport localport tls)) |
| 244 |
(setq pro (apply 'start-process name nil mew-prog-ssl opts)) |
| 245 |
;; An error would occur. So, let's exit in the case. |
| 246 |
(cond |
| 247 |
((not (processp pro)) |
| 248 |
(message "Creating an SSL/TLS connection...FAILED") |
| 249 |
(throw 'loop nil)) |
| 250 |
((not (memq (process-status pro) '(run))) |
| 251 |
(delete-process pro) |
| 252 |
(message "Creating an SSL/TLS connection...FAILED") |
| 253 |
(throw 'loop nil))) |
| 254 |
;; stunnel is now running. |
| 255 |
(mew-process-silent-exit pro) |
| 256 |
(setq pnm (process-name pro)) |
| 257 |
(mew-info-clean-up pnm) |
| 258 |
(mew-ssl-set-try pnm 0) |
| 259 |
(if (>= mew-ssl-ver 4) (mew-ssl-set-file pnm (car opts))) |
| 260 |
(mew-set-process-cs pro mew-cs-text-for-read mew-cs-text-for-write) |
| 261 |
(set-process-filter pro 'mew-ssl-filter1) |
| 262 |
(set-process-sentinel pro 'mew-ssl-sentinel) |
| 263 |
(mew-rendezvous (null (mew-ssl-get-status pnm))) |
| 264 |
(if (eq (mew-ssl-get-status pnm) t) |
| 265 |
(throw 'loop (setq bound t))) |
| 266 |
;; bind-failure |
| 267 |
(setq localport (1+ localport)))) |
| 268 |
(mew-ssl-set-status pnm nil) |
| 269 |
(if (not bound) |
| 270 |
(progn |
| 271 |
(message "Creating an SSL/TLS connection...FAILED") |
| 272 |
nil) |
| 273 |
;; "stunnel" does not gain access to the remote port |
| 274 |
;; until a tunneled connection is created. |
| 275 |
;; So, we need to check the SSL/TLS tunnel with a dummy |
| 276 |
;; tunneled connection here. |
| 277 |
(set-process-filter pro 'mew-ssl-filter2) |
| 278 |
(setq dummy (open-network-stream " *Mew dummy*" nil mew-ssl-localhost localport)) |
| 279 |
(mew-rendezvous (null (mew-ssl-get-status pnm))) |
| 280 |
(if (processp dummy) (delete-process dummy)) |
| 281 |
(if (eq (mew-ssl-get-status pnm) t) |
| 282 |
(progn |
| 283 |
(message "Creating an SSL/TLS connection...done") |
| 284 |
(set-process-filter pro 'mew-ssl-filter3) |
| 285 |
pro) |
| 286 |
;; verify-failure |
| 287 |
(delete-process pro) |
| 288 |
(message "Creating an SSL/TLS connection...FAILED (cert verify failure)") |
| 289 |
nil))))))) |
| 290 |
|
| 291 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 292 |
;;; |
| 293 |
;;; Filter and sentinel |
| 294 |
;;; |
| 295 |
|
| 296 |
(defun mew-ssl-debug (label string) |
| 297 |
(when (mew-debug 'net) |
| 298 |
(with-current-buffer (get-buffer-create mew-buffer-debug) |
| 299 |
(goto-char (point-max)) |
| 300 |
(insert (format "\n<%s>\n%s\n" label string))))) |
| 301 |
|
| 302 |
(defun mew-ssl-filter1 (process string) |
| 303 |
(let* ((pnm (process-name process)) |
| 304 |
(prev-str (mew-ssl-get-string pnm))) |
| 305 |
(save-excursion |
| 306 |
(mew-ssl-debug "SSL/TLS: " string) |
| 307 |
(mew-ssl-set-string pnm string) |
| 308 |
(setq string (concat prev-str string)) |
| 309 |
(cond |
| 310 |
((string-match "bound \\(\\|FD=[0-9]+ \\)to" string) |
| 311 |
(mew-ssl-set-status pnm t)) |
| 312 |
((string-match "gethostbyname: Valid name, no data record of requested type" string) |
| 313 |
(mew-ssl-set-status pnm 'gethostbyname-failure)) |
| 314 |
((string-match "gethostbyname: Host not found" string) |
| 315 |
(mew-ssl-set-status pnm 'gethostbyname-failure)) |
| 316 |
((string-match "Local: bind: Address already in use" string) |
| 317 |
(mew-ssl-set-status pnm 'bind-failure)))))) |
| 318 |
|
| 319 |
(defun mew-ssl-filter2 (process string) |
| 320 |
(let* ((pnm (process-name process)) |
| 321 |
(prev-str (mew-ssl-get-string pnm))) |
| 322 |
(save-excursion |
| 323 |
(mew-ssl-debug "SSL/TLS: " string) |
| 324 |
(mew-ssl-set-string pnm string) |
| 325 |
(setq string (concat prev-str string)) |
| 326 |
(cond |
| 327 |
((string-match "Negotiated \\| ciphersuite:\\|opened with SSL" string) |
| 328 |
(mew-ssl-set-status pnm t)) |
| 329 |
((string-match "Failed to initialize" string) |
| 330 |
(mew-ssl-set-status pnm t)) ;; xxx |
| 331 |
((string-match "verify failed" string) |
| 332 |
(mew-ssl-set-status pnm 'verify-failure)))))) |
| 333 |
|
| 334 |
(defun mew-ssl-filter3 (_process string) |
| 335 |
(save-excursion |
| 336 |
(mew-ssl-debug "SSL/TLS: " string))) |
| 337 |
|
| 338 |
(defun mew-ssl-sentinel (process _event) |
| 339 |
(let* ((pnm (process-name process)) |
| 340 |
(file (mew-ssl-get-file pnm))) |
| 341 |
(save-excursion |
| 342 |
(mew-delete-file file)))) |
| 343 |
|
| 344 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 345 |
;;; |
| 346 |
;;; stunnel version check |
| 347 |
;;; |
| 348 |
|
| 349 |
(defun mew-ssl-setup () |
| 350 |
(if (not (mew-which-exec mew-prog-ssl)) |
| 351 |
(setq mew-ssl-ver nil) |
| 352 |
(with-temp-buffer |
| 353 |
(call-process mew-prog-ssl nil t nil "-help") |
| 354 |
(goto-char (point-min)) |
| 355 |
(re-search-forward "^stunnel " nil t 1) |
| 356 |
(if (looking-at "\\([45]\\)\\.\\([0-9]+\\)") |
| 357 |
(progn |
| 358 |
(setq mew-ssl-ver (string-to-number (mew-match-string 1))) |
| 359 |
(setq mew-ssl-minor-ver (string-to-number (mew-match-string 2)))) |
| 360 |
(setq mew-ssl-ver 3)) |
| 361 |
(when (re-search-forward "LIBWRAP" nil t) |
| 362 |
(setq mew-ssl-libwrap t)) |
| 363 |
(when (re-search-forward "foreground" nil t) |
| 364 |
(setq mew-ssl-unixlike t)) |
| 365 |
(when (re-search-forward "checkHost" nil t) |
| 366 |
(setq mew-ssl-checkhost t))))) |
| 367 |
|
| 368 |
(provide 'mew-ssl) |
| 369 |
|
| 370 |
;;; Copyright Notice: |
| 371 |
|
| •372 |
;; Copyright (C) 2002-2020 Mew developing team. |
| 373 |
;; All rights reserved. |
| 374 |
|
| 375 |
;; Redistribution and use in source and binary forms, with or without |
| 376 |
;; modification, are permitted provided that the following conditions |
| 377 |
;; are met: |
| 378 |
;; |
| 379 |
;; 1. Redistributions of source code must retain the above copyright |
| 380 |
;; notice, this list of conditions and the following disclaimer. |
| 381 |
;; 2. Redistributions in binary form must reproduce the above copyright |
| 382 |
;; notice, this list of conditions and the following disclaimer in the |
| 383 |
;; documentation and/or other materials provided with the distribution. |
| 384 |
;; 3. Neither the name of the team nor the names of its contributors |
| 385 |
;; may be used to endorse or promote products derived from this software |
| 386 |
;; without specific prior written permission. |
| 387 |
;; |
| 388 |
;; THIS SOFTWARE IS PROVIDED BY THE TEAM AND CONTRIBUTORS ``AS IS'' AND |
| 389 |
;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 390 |
;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 391 |
;; PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TEAM OR CONTRIBUTORS BE |
| 392 |
;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 393 |
;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 394 |
;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 395 |
;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 396 |
;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 397 |
;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 398 |
;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 399 |
|
| 400 |
;;; mew-ssl.el ends here |