Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
marvel
GitHub Repository: marvel/qnf
Path: blob/master/elisp/ssl.el
987 views
1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML>
2
<HEAD>
3
<!-- hennerik CVSweb $Revision: 1.112 $ -->
4
<TITLE>gnus/contrib/ssl.el - view - 7.6</TITLE></HEAD>
5
<BODY BGCOLOR="#eeeeee">
6
<table width="100%" border=0 cellspacing=0 cellpadding=1 bgcolor="#9999ee"><tr valign=bottom><td><a href="ssl.el#rev7.6"><IMG SRC="/icons/back.gif" ALT="[BACK]" BORDER="0" WIDTH="20" HEIGHT="22"></a> <b>Return to <A HREF="ssl.el#rev7.6">ssl.el</A>
7
CVS log</b> <IMG SRC="/icons/text.gif" ALT="[TXT]" BORDER="0" WIDTH="20" HEIGHT="22"></td><td align=right><IMG SRC="/icons/dir.gif" ALT="[DIR]" BORDER="0" WIDTH="20" HEIGHT="22"> <b>Up to <a href="/cgi-bin/cvsweb.cgi/#dirlist">[(ding)]</a> / <a href="/cgi-bin/cvsweb.cgi/gnus/#dirlist">gnus</a> / <a href="/cgi-bin/cvsweb.cgi/gnus/contrib/#dirlist">contrib</a></b></td></tr></table><HR noshade><table width="100%"><tr><td bgcolor="#ffffff">File: <a href="/cgi-bin/cvsweb.cgi/#dirlist">[(ding)]</a> / <a href="/cgi-bin/cvsweb.cgi/gnus/#dirlist">gnus</a> / <a href="/cgi-bin/cvsweb.cgi/gnus/contrib/#dirlist">contrib</a> / <a href="/cgi-bin/cvsweb.cgi/gnus/contrib/ssl.el">ssl.el</a>&nbsp;(<A HREF="/cgi-bin/cvsweb.cgi/~checkout~/gnus/contrib/ssl.el?rev=7.6" target="cvs_checkout" onClick="window.open('/cgi-bin/cvsweb.cgi/~checkout~/gnus/contrib/ssl.el?rev=7.6','cvs_checkout','resizeable,scrollbars');"><b>download</b></A>)<BR>
8
Revision <B>7.6</B>, <i>Thu Dec 29 03:46:41 2005 UTC</i> (4 years, 5 months ago) by <i>miles</i>
9
<BR>CVS Tags: <b>n0-6, n0-5, n0-4</b><BR>Changes since <b>7.5: +2 -0
10
lines</b><PRE>
11
Add arch tagline
12
</PRE>
13
</td></tr></table><HR noshade><PRE>;;; ssl.el,v --- ssl functions for Emacsen without them builtin
14
;; Author: William M. Perry &lt;<A HREF="mailto:[email protected]">[email protected]</A>&gt;
15
;; $Revision: 1.5 $
16
;; Keywords: comm
17
18
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
19
;;; Copyright (c) 1995, 1996 by William M. Perry &lt;<A HREF="mailto:[email protected]">[email protected]</A>&gt;
20
;;; Copyright (c) 1996, 97, 98, 99, 2001 Free Software Foundation, Inc.
21
;;;
22
;;; This file is part of GNU Emacs.
23
;;;
24
;;; GNU Emacs is free software; you can redistribute it and/or modify
25
;;; it under the terms of the GNU General Public License as published by
26
;;; the Free Software Foundation; either version 2, or (at your option)
27
;;; any later version.
28
;;;
29
;;; GNU Emacs is distributed in the hope that it will be useful,
30
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
31
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. &nbsp;See the
32
;;; GNU General Public License for more details.
33
;;;
34
;;; You should have received a copy of the GNU General Public License
35
;;; along with GNU Emacs; see the file COPYING. &nbsp;If not, write to the
36
;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
37
;;; Boston, MA 02111-1307, USA.
38
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
39
40
(eval-when-compile (require 'cl))
41
(require 'base64)
42
(require 'url) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;; for `url-configuration-directory'
43
44
(defgroup ssl nil
45
&nbsp;&quot;Support for `Secure Sockets Layer' encryption.&quot;
46
&nbsp;:group 'comm)
47
&nbsp;
48
(defcustom ssl-certificate-directory &quot;~/.w3/certs/&quot;
49
&nbsp;&quot;*Directory in which to store CA certificates.&quot;
50
&nbsp;:group 'ssl
51
&nbsp;:type 'directory)
52
53
(defcustom ssl-rehash-program-name &quot;c_rehash&quot;
54
&nbsp;&quot;*Program to run after adding a cert to a directory .
55
Run with one argument, the directory name.&quot;
56
&nbsp;:group 'ssl
57
&nbsp;:type 'string)
58
59
(defcustom ssl-view-certificate-program-name &quot;x509&quot;
60
&nbsp;&quot;*The program to run to provide a human-readable view of a certificate.&quot;
61
&nbsp;:group 'ssl
62
&nbsp;:type 'string)
63
64
(defcustom ssl-view-certificate-program-arguments '(&quot;-text&quot; &quot;-inform&quot; &quot;DER&quot;)
65
&nbsp;&quot;*Arguments that should be passed to the certificate viewing program.
66
The certificate is piped to it.
67
Maybe a way of passing a file should be implemented&quot;
68
&nbsp;:group 'ssl
69
&nbsp;:type '(repeat string))
70
71
(defcustom ssl-certificate-directory-style 'ssleay
72
&nbsp;&quot;*Style of cert database to use, the only valid value right now is `ssleay'.
73
This means a directory of pem encoded certificates with hash symlinks.&quot;
74
&nbsp;:group 'ssl
75
&nbsp;:type '(choice (const :tag &quot;SSLeay&quot; :value ssleay)
76
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (const :tag &quot;OpenSSL&quot; :value openssl)))
77
78
(defcustom ssl-certificate-verification-policy 0
79
&nbsp;&quot;*How far up the certificate chain we should verify.&quot;
80
&nbsp;:group 'ssl
81
&nbsp;:type '(choice (const :tag &quot;No verification&quot; :value 0)
82
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (const :tag &quot;Verification required&quot; :value 1)
83
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (const :tag &quot;Reject connection if verification fails&quot; :value 3)
84
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (const :tag &quot;SSL_VERIFY_CLIENT_ONCE&quot; :value 5)))
85
86
(defcustom ssl-program-name &quot;openssl&quot;
87
&nbsp;&quot;*The program to run in a subprocess to open an SSL connection.&quot;
88
&nbsp;:group 'ssl
89
&nbsp;:type 'string)
90
91
(defcustom ssl-program-arguments
92
&nbsp;'(&quot;s_client&quot;
93
&nbsp; &nbsp;&quot;-quiet&quot;
94
&nbsp; &nbsp;&quot;-host&quot; host
95
&nbsp; &nbsp;&quot;-port&quot; service
96
&nbsp; &nbsp;&quot;-verify&quot; (int-to-string ssl-certificate-verification-policy)
97
&nbsp; &nbsp;&quot;-CApath&quot; ssl-certificate-directory
98
&nbsp; &nbsp;)
99
&nbsp;&quot;*Arguments that should be passed to the program `ssl-program-name'.
100
This should be used if your SSL program needs command line switches to
101
specify any behaviour (certificate file locations, etc).
102
The special symbols 'host and 'port may be used in the list of arguments
103
and will be replaced with the hostname and service/port that will be connected
104
to.&quot;
105
&nbsp;:group 'ssl
106
&nbsp;:type 'list)
107
108
(defcustom ssl-view-certificate-program-name ssl-program-name
109
&nbsp;&quot;*The program to run to provide a human-readable view of a certificate.&quot;
110
&nbsp;:group 'ssl
111
&nbsp;:type 'string)
112
113
(defcustom ssl-view-certificate-program-arguments
114
&nbsp;'(&quot;x509&quot; &quot;-text&quot; &quot;-inform&quot; &quot;DER&quot;)
115
&nbsp;&quot;*Arguments that should be passed to the certificate viewing program.
116
The certificate is piped to it.
117
Maybe a way of passing a file should be implemented.&quot;
118
&nbsp;:group 'ssl
119
&nbsp;:type 'list)
120
121
(defun ssl-certificate-information (der)
122
&nbsp;&quot;Return an assoc list of information about a certificate in DER format.&quot;
123
&nbsp;(let ((certificate (concat &quot;-----BEGIN CERTIFICATE-----\n&quot;
124
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (base64-encode-string der)
125
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;\n-----END CERTIFICATE-----\n&quot;))
126
&nbsp; &nbsp; &nbsp; &nbsp;(exit-code 0))
127
&nbsp; &nbsp;(save-excursion
128
&nbsp; &nbsp; &nbsp;(set-buffer (get-buffer-create &quot; *openssl*&quot;))
129
&nbsp; &nbsp; &nbsp;(erase-buffer)
130
&nbsp; &nbsp; &nbsp;(insert certificate)
131
&nbsp; &nbsp; &nbsp;(setq exit-code
132
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(condition-case ()
133
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(call-process-region (point-min) (point-max)
134
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ssl-program-name
135
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t (list (current-buffer) nil) t
136
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;x509&quot;
137
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;-subject&quot; ; Print the subject DN
138
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;-issuer&quot; ; Print the issuer DN
139
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;-dates&quot; ; Both before and after dates
140
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;-serial&quot; ; print out serial number
141
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;-noout&quot; ; Don't spit out the certificate
142
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )
143
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(error -1)))
144
&nbsp; &nbsp; &nbsp;(if (/= exit-code 0)
145
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nil
146
&nbsp; &nbsp; &nbsp; &nbsp;(let ((vals nil))
147
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(goto-char (point-min))
148
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(while (re-search-forward &quot;^\\([^=\n\r]+\\)\\s *=\\s *\\(.*\\)&quot; nil t)
149
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(push (cons (match-string 1) (match-string 2)) vals))
150
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;vals)))))
151
&nbsp;
152
(defun ssl-accept-ca-certificate ()
153
&nbsp;&quot;Ask if the user is willing to accept a new CA certificate.
154
The buffer name should be the intended name of the certificate, and
155
the buffer should probably be in DER encoding&quot;
156
&nbsp;;; TODO, check if it is really new or if we already know it
157
&nbsp;(let* ((process-connection-type nil)
158
&nbsp; &nbsp; &nbsp; &nbsp; (tmpbuf (generate-new-buffer &quot;X509 CA Certificate Information&quot;))
159
&nbsp; &nbsp; &nbsp; &nbsp; (response (save-excursion
160
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (and (eq 0
161
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(apply 'call-process-region
162
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (point-min) (point-max)
163
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ssl-view-certificate-program-name
164
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nil tmpbuf t
165
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ssl-view-certificate-program-arguments))
166
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(switch-to-buffer tmpbuf)
167
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(goto-char (point-min))
168
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(or (recenter) t)
169
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(yes-or-no-p
170
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;Accept this CA to vouch for secure server identities? &quot;)
171
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(kill-buffer tmpbuf)))))
172
&nbsp; &nbsp;(if (not response)
173
&nbsp; &nbsp; &nbsp; &nbsp;nil
174
&nbsp; &nbsp; &nbsp;(if (not (file-directory-p ssl-certificate-directory))
175
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(make-directory ssl-certificate-directory))
176
&nbsp; &nbsp; &nbsp;(case ssl-certificate-directory-style
177
&nbsp; &nbsp; &nbsp; &nbsp;(ssleay
178
&nbsp; &nbsp; &nbsp; &nbsp; (base64-encode-region (point-min) (point-max))
179
&nbsp; &nbsp; &nbsp; &nbsp; (goto-char (point-min))
180
&nbsp; &nbsp; &nbsp; &nbsp; (insert &quot;-----BEGIN CERTIFICATE-----\n&quot;)
181
&nbsp; &nbsp; &nbsp; &nbsp; (goto-char (point-max))
182
&nbsp; &nbsp; &nbsp; &nbsp; (insert &quot;-----END CERTIFICATE-----\n&quot;)
183
&nbsp; &nbsp; &nbsp; &nbsp; (let ((f (expand-file-name
184
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (concat (file-name-sans-extension (buffer-name)) &quot;.pem&quot;)
185
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ssl-certificate-directory)))
186
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (write-file f)
187
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (call-process ssl-rehash-program-name
188
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nil nil nil
189
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (expand-file-name ssl-certificate-directory))))))))
190
191
(defvar ssl-exec-wrapper nil)
192
193
(defun ssl-get-command ()
194
&nbsp;(if (memq system-type '(ms-dos ms-windows axp-vms vax-vms))
195
&nbsp; &nbsp; &nbsp;;; Nothing to do on DOS, Windows, or VMS!
196
&nbsp; &nbsp; &nbsp;(cons ssl-program-name ssl-program-arguments)
197
&nbsp; &nbsp;(if (not ssl-exec-wrapper)
198
&nbsp; &nbsp; &nbsp; &nbsp;(let ((script
199
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (expand-file-name &quot;exec_ssl_quietly&quot; url-configuration-directory)))
200
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(if (not (file-executable-p script))
201
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;;; Need to create our handy-dandy utility script to shut OpenSSL
202
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;;; up completely.
203
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(progn
204
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(write-region &quot;#!/bin/sh\n\nexec \&quot;$@\&quot; 2&gt; /dev/null\n&quot; nil
205
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;script nil 5)
206
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(set-file-modes script 493))) ; (rwxr-xr-x)
207
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(setq ssl-exec-wrapper script)))
208
&nbsp; &nbsp;(cons ssl-exec-wrapper (cons ssl-program-name ssl-program-arguments))))
209
210
(defun open-ssl-stream (name buffer host service)
211
&nbsp;&quot;Open a SSL connection for a service to a host.
212
Returns a subprocess-object to represent the connection.
213
Input and output work as for subprocesses; `delete-process' closes it.
214
Args are NAME BUFFER HOST SERVICE.
215
NAME is name for process. &nbsp;It is modified if necessary to make it unique.
216
BUFFER is the buffer (or buffer name) to associate with the process.
217
Process output goes at end of that buffer, unless you specify
218
an output stream or filter function to handle the output.
219
BUFFER may be also nil, meaning that this process is not associated
220
with any buffer.
221
Third arg is name of the host to connect to, or its IP address.
222
Fourth arg SERVICE is name of the service desired, or an integer
223
specifying a port number to connect to.&quot;
224
&nbsp;(if (integerp service) (setq service (int-to-string service)))
225
&nbsp;(let* ((process-connection-type nil)
226
&nbsp; &nbsp; &nbsp; &nbsp; (port service)
227
&nbsp; &nbsp; &nbsp; &nbsp; (proc (eval `(start-process name buffer ,@(ssl-get-command)))))
228
&nbsp; &nbsp;(process-kill-without-query proc)
229
&nbsp; &nbsp;proc))
230
231
(provide 'ssl)
232
233
;; arch-tag: 659fae92-1c67-4055-939f-32153c2f5114
234
</PRE>
235