gnutls.el 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. ;;; gnutls.el --- Support SSL/TLS connections through GnuTLS
  2. ;; Copyright (C) 2010-2012 Free Software Foundation, Inc.
  3. ;; Author: Ted Zlatanov <tzz@lifelogs.com>
  4. ;; Keywords: comm, tls, ssl, encryption
  5. ;; Originally-By: Simon Josefsson (See http://josefsson.org/emacs-security/)
  6. ;; Thanks-To: Lars Magne Ingebrigtsen <larsi@gnus.org>
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; This package provides language bindings for the GnuTLS library
  20. ;; using the corresponding core functions in gnutls.c. It should NOT
  21. ;; be used directly, only through open-protocol-stream.
  22. ;; Simple test:
  23. ;;
  24. ;; (open-gnutls-stream "tls" "tls-buffer" "yourserver.com" "https")
  25. ;; (open-gnutls-stream "tls" "tls-buffer" "imap.gmail.com" "imaps")
  26. ;;; Code:
  27. (eval-when-compile (require 'cl))
  28. (defgroup gnutls nil
  29. "Emacs interface to the GnuTLS library."
  30. :version "24.1"
  31. :prefix "gnutls-"
  32. :group 'net-utils)
  33. (defcustom gnutls-algorithm-priority nil
  34. "If non-nil, this should be a TLS priority string.
  35. For instance, if you want to skip the \"dhe-rsa\" algorithm,
  36. set this variable to \"normal:-dhe-rsa\"."
  37. :group 'gnutls
  38. :type '(choice (const nil)
  39. string))
  40. (defcustom gnutls-trustfiles
  41. '(
  42. "/etc/ssl/certs/ca-certificates.crt" ; Debian, Ubuntu, Gentoo and Arch Linux
  43. "/etc/pki/tls/certs/ca-bundle.crt" ; Fedora and RHEL
  44. "/etc/ssl/ca-bundle.pem" ; Suse
  45. "/usr/ssl/certs/ca-bundle.crt" ; Cygwin
  46. )
  47. "List of CA bundle location filenames or a function returning said list.
  48. The files may be in PEM or DER format, as per the GnuTLS documentation.
  49. The files may not exist, in which case they will be ignored."
  50. :group 'gnutls
  51. :type '(choice (function :tag "Function to produce list of bundle filenames")
  52. (repeat (file :tag "Bundle filename"))))
  53. ;;;###autoload
  54. (defcustom gnutls-min-prime-bits 256
  55. ;; Several mail servers send fewer bits than the GnuTLS default.
  56. ;; Currently, 256 appears to be a reasonable choice (Bug#11267).
  57. "Minimum number of prime bits accepted by GnuTLS for key exchange.
  58. During a Diffie-Hellman handshake, if the server sends a prime
  59. number with fewer than this number of bits, the handshake is
  60. rejected. \(The smaller the prime number, the less secure the
  61. key exchange is against man-in-the-middle attacks.)
  62. A value of nil says to use the default GnuTLS value."
  63. :type '(choice (const :tag "Use default value" nil)
  64. (integer :tag "Number of bits" 512))
  65. :group 'gnutls)
  66. (defun open-gnutls-stream (name buffer host service)
  67. "Open a SSL/TLS connection for a service to a host.
  68. Returns a subprocess-object to represent the connection.
  69. Input and output work as for subprocesses; `delete-process' closes it.
  70. Args are NAME BUFFER HOST SERVICE.
  71. NAME is name for process. It is modified if necessary to make it unique.
  72. BUFFER is the buffer (or `buffer-name') to associate with the process.
  73. Process output goes at end of that buffer, unless you specify
  74. an output stream or filter function to handle the output.
  75. BUFFER may be also nil, meaning that this process is not associated
  76. with any buffer
  77. Third arg is name of the host to connect to, or its IP address.
  78. Fourth arg SERVICE is name of the service desired, or an integer
  79. specifying a port number to connect to.
  80. Usage example:
  81. \(with-temp-buffer
  82. \(open-gnutls-stream \"tls\"
  83. \(current-buffer)
  84. \"your server goes here\"
  85. \"imaps\"))
  86. This is a very simple wrapper around `gnutls-negotiate'. See its
  87. documentation for the specific parameters you can use to open a
  88. GnuTLS connection, including specifying the credential type,
  89. trust and key files, and priority string."
  90. (gnutls-negotiate :process (open-network-stream name buffer host service)
  91. :type 'gnutls-x509pki
  92. :hostname host))
  93. (put 'gnutls-error
  94. 'error-conditions
  95. '(error gnutls-error))
  96. (put 'gnutls-error
  97. 'error-message "GnuTLS error")
  98. (declare-function gnutls-boot "gnutls.c" (proc type proplist))
  99. (declare-function gnutls-errorp "gnutls.c" (error))
  100. (defun* gnutls-negotiate
  101. (&rest spec
  102. &key process type hostname priority-string
  103. trustfiles crlfiles keylist min-prime-bits
  104. verify-flags verify-error verify-hostname-error
  105. &allow-other-keys)
  106. "Negotiate a SSL/TLS connection. Returns proc. Signals gnutls-error.
  107. Note arguments are passed CL style, :type TYPE instead of just TYPE.
  108. TYPE is `gnutls-x509pki' (default) or `gnutls-anon'. Use nil for the default.
  109. PROCESS is a process returned by `open-network-stream'.
  110. HOSTNAME is the remote hostname. It must be a valid string.
  111. PRIORITY-STRING is as per the GnuTLS docs, default is \"NORMAL\".
  112. TRUSTFILES is a list of CA bundles. It defaults to `gnutls-trustfiles'.
  113. CRLFILES is a list of CRL files.
  114. KEYLIST is an alist of (client key file, client cert file) pairs.
  115. MIN-PRIME-BITS is the minimum acceptable size of Diffie-Hellman keys
  116. \(see `gnutls-min-prime-bits' for more information). Use nil for the
  117. default.
  118. When VERIFY-HOSTNAME-ERROR is not nil, an error will be raised
  119. when the hostname does not match the presented certificate's host
  120. name. The exact verification algorithm is a basic implementation
  121. of the matching described in RFC2818 (HTTPS), which takes into
  122. account wildcards, and the DNSName/IPAddress subject alternative
  123. name PKIX extension. See GnuTLS' gnutls_x509_crt_check_hostname
  124. for details. When VERIFY-HOSTNAME-ERROR is nil, only a warning
  125. will be issued.
  126. When VERIFY-ERROR is not nil, an error will be raised when the
  127. peer certificate verification fails as per GnuTLS'
  128. gnutls_certificate_verify_peers2. Otherwise, only warnings will
  129. be shown about the verification failure.
  130. VERIFY-FLAGS is a numeric OR of verification flags only for
  131. `gnutls-x509pki' connections. See GnuTLS' x509.h for details;
  132. here's a recent version of the list.
  133. GNUTLS_VERIFY_DISABLE_CA_SIGN = 1,
  134. GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT = 2,
  135. GNUTLS_VERIFY_DO_NOT_ALLOW_SAME = 4,
  136. GNUTLS_VERIFY_ALLOW_ANY_X509_V1_CA_CRT = 8,
  137. GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD2 = 16,
  138. GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD5 = 32,
  139. GNUTLS_VERIFY_DISABLE_TIME_CHECKS = 64,
  140. GNUTLS_VERIFY_DISABLE_TRUSTED_TIME_CHECKS = 128,
  141. GNUTLS_VERIFY_DO_NOT_ALLOW_X509_V1_CA_CRT = 256
  142. It must be omitted, a number, or nil; if omitted or nil it
  143. defaults to GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT."
  144. (let* ((type (or type 'gnutls-x509pki))
  145. (trustfiles (or trustfiles
  146. (delq nil
  147. (mapcar (lambda (f) (and f (file-exists-p f) f))
  148. (if (functionp gnutls-trustfiles)
  149. (funcall gnutls-trustfiles)
  150. gnutls-trustfiles)))))
  151. (priority-string (or priority-string
  152. (cond
  153. ((eq type 'gnutls-anon)
  154. "NORMAL:+ANON-DH:!ARCFOUR-128")
  155. ((eq type 'gnutls-x509pki)
  156. (if gnutls-algorithm-priority
  157. (upcase gnutls-algorithm-priority)
  158. "NORMAL")))))
  159. (min-prime-bits (or min-prime-bits gnutls-min-prime-bits))
  160. (params `(:priority ,priority-string
  161. :hostname ,hostname
  162. :loglevel ,gnutls-log-level
  163. :min-prime-bits ,min-prime-bits
  164. :trustfiles ,trustfiles
  165. :crlfiles ,crlfiles
  166. :keylist ,keylist
  167. :verify-flags ,verify-flags
  168. :verify-error ,verify-error
  169. :verify-hostname-error ,verify-hostname-error
  170. :callbacks nil))
  171. ret)
  172. (gnutls-message-maybe
  173. (setq ret (gnutls-boot process type params))
  174. "boot: %s" params)
  175. (when (gnutls-errorp ret)
  176. ;; This is a error from the underlying C code.
  177. (signal 'gnutls-error (list process ret)))
  178. process))
  179. (declare-function gnutls-error-string "gnutls.c" (error))
  180. (defun gnutls-message-maybe (doit format &rest params)
  181. "When DOIT, message with the caller name followed by FORMAT on PARAMS."
  182. ;; (apply 'debug format (or params '(nil)))
  183. (when (gnutls-errorp doit)
  184. (message "%s: (err=[%s] %s) %s"
  185. "gnutls.el"
  186. doit (gnutls-error-string doit)
  187. (apply 'format format (or params '(nil))))))
  188. (provide 'gnutls)
  189. ;;; gnutls.el ends here