1 ;;; ssl.el,v --- ssl functions for emacsen without them builtin
4 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5 ;;; Copyright (c) 1995, 1996 by William M. Perry <wmperry@cs.indiana.edu>
6 ;;; Copyright (c) 1996 - 1999 Free Software Foundation, Inc.
8 ;;; This file is part of GNU Emacs.
10 ;;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;;; it under the terms of the GNU General Public License as published by
12 ;;; the Free Software Foundation; either version 2, or (at your option)
13 ;;; any later version.
15 ;;; GNU Emacs is distributed in the hope that it will be useful,
16 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;;; GNU General Public License for more details.
20 ;;; You should have received a copy of the GNU General Public License
21 ;;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;;; Boston, MA 02111-1307, USA.
24 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26 (eval-when-compile (require 'cl))
33 (if (and (featurep 'custom) (fboundp 'custom-declare-variable))
34 nil ;; We've got what we needed
35 ;; We have the old custom-library, hack around it!
36 (defmacro defgroup (&rest args)
38 (defmacro defcustom (var value doc &rest args)
39 (` (defvar (, var) (, value) (, doc))))))
42 "Support for `Secure Sockets Layer' encryption."
45 (defcustom ssl-certificate-directory "~/.w3/certs/"
46 "*Directory to store CA certificates in"
50 (defcustom ssl-rehash-program-name "c_rehash"
51 "*Program to run after adding a cert to a directory .
52 Run with one argument, the directory name."
56 (defcustom ssl-view-certificate-program-name "x509"
57 "*The program to run to provide a human-readable view of a certificate."
61 (defcustom ssl-view-certificate-program-arguments '("-text" "-inform" "DER")
62 "*Arguments that should be passed to the certificate viewing program.
63 The certificate is piped to it.
64 Maybe a way of passing a file should be implemented"
68 (defcustom ssl-certificate-directory-style 'ssleay
69 "*Style of cert database to use, the only valid value right now is `ssleay'.
70 This means a directory of pem encoded certificates with hash symlinks."
72 :type '(choice (const :tag "SSLeay" :value ssleay)
73 (const :tag "OpenSSL" :value openssl)))
75 (defcustom ssl-certificate-verification-policy 0
76 "*How far up the certificate chain we should verify."
78 :type '(choice (const :tag "No verification" :value 0)
79 (const :tag "Verification required" :value 1)
80 (const :tag "Reject connection if verification fails" :value 3)
81 (const :tag "SSL_VERIFY_CLIENT_ONCE" :value 5)))
83 (defcustom ssl-program-name "openssl"
84 "*The program to run in a subprocess to open an SSL connection."
88 (defcustom ssl-program-arguments
93 "-verify" (int-to-string ssl-certificate-verification-policy)
94 "-CApath" ssl-certificate-directory
96 "*Arguments that should be passed to the program `ssl-program-name'.
97 This should be used if your SSL program needs command line switches to
98 specify any behaviour (certificate file locations, etc).
99 The special symbols 'host and 'port may be used in the list of arguments
100 and will be replaced with the hostname and service/port that will be connected
105 (defun ssl-certificate-information (der)
106 "Return an assoc list of information about a certificate in DER format."
107 (let ((certificate (concat "-----BEGIN CERTIFICATE-----\n"
108 (base64-encode-string der)
109 "\n-----END CERTIFICATE-----\n"))
112 (set-buffer (get-buffer-create " *openssl*"))
115 (setq exit-code (condition-case ()
116 (call-process-region (point-min) (point-max)
118 t (list (current-buffer) nil) t
120 "-subject" ; Print the subject DN
121 "-issuer" ; Print the issuer DN
122 "-dates" ; Both before and after dates
123 "-serial" ; print out serial number
124 "-noout" ; Don't spit out the certificate
130 (goto-char (point-min))
131 (while (re-search-forward "^\\([^=\n\r]+\\)\\s *=\\s *\\(.*\\)" nil t)
132 (push (cons (match-string 1) (match-string 2)) vals))
135 (defun ssl-accept-ca-certificate ()
136 "Ask if the user is willing to accept a new CA certificate. The buffer-name
137 should be the intended name of the certificate, and the buffer should probably
139 ;; TODO, check if it is really new or if we already know it
140 (let* ((process-connection-type nil)
141 (tmpbuf (generate-new-buffer "X509 CA Certificate Information"))
142 (response (save-excursion
144 (apply 'call-process-region
145 (point-min) (point-max)
146 ssl-view-certificate-program-name
148 ssl-view-certificate-program-arguments))
149 (switch-to-buffer tmpbuf)
150 (goto-char (point-min))
153 "Accept this CA to vouch for secure server identities? ")
154 (kill-buffer tmpbuf)))))
157 (if (not (file-directory-p ssl-certificate-directory))
158 (make-directory ssl-certificate-directory))
159 (case ssl-certificate-directory-style
161 (base64-encode-region (point-min) (point-max))
162 (goto-char (point-min))
163 (insert "-----BEGIN CERTIFICATE-----\n")
164 (goto-char (point-max))
165 (insert "-----END CERTIFICATE-----\n")
166 (let ((f (expand-file-name
167 (concat (file-name-sans-extension (buffer-name)) ".pem")
168 ssl-certificate-directory)))
170 (call-process ssl-rehash-program-name
172 (expand-file-name ssl-certificate-directory))))))))
174 (defun open-ssl-stream (name buffer host service)
175 "Open a SSL connection for a service to a host.
176 Returns a subprocess-object to represent the connection.
177 Input and output work as for subprocesses; `delete-process' closes it.
178 Args are NAME BUFFER HOST SERVICE.
179 NAME is name for process. It is modified if necessary to make it unique.
180 BUFFER is the buffer (or buffer-name) to associate with the process.
181 Process output goes at end of that buffer, unless you specify
182 an output stream or filter function to handle the output.
183 BUFFER may be also nil, meaning that this process is not associated
185 Third arg is name of the host to connect to, or its IP address.
186 Fourth arg SERVICE is name of the service desired, or an integer
187 specifying a port number to connect to."
188 (if (integerp service) (setq service (int-to-string service)))
189 (let* ((process-connection-type nil)
193 (start-process name buffer ssl-program-name
194 (,@ ssl-program-arguments))))))
195 (process-kill-without-query proc)