Debian Codegraph @

ext/tls/tls.scm

raw
1 ;;;
2 ;;; tls - TLS secure connection interface
3 ;;;
4 ;;; Copyright (c) 2011 Kirill Zorin <[email protected]>
5 ;;;
6 ;;; Redistribution and use in source and binary forms, with or without
7 ;;; modification, are permitted provided that the following conditions
8 ;;; are met:
9 ;;;
10 ;;; 1. Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
12 ;;;
13 ;;; 2. Redistributions in binary form must reproduce the above copyright
14 ;;; notice, this list of conditions and the following disclaimer in the
15 ;;; documentation and/or other materials provided with the distribution.
16 ;;;
17 ;;; 3. Neither the name of the authors nor the names of its contributors
18 ;;; may be used to endorse or promote products derived from this
19 ;;; software without specific prior written permission.
20 ;;;
21 ;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 ;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 ;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 ;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 ;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 ;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 ;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 ;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 ;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 ;;;
33
34 #!no-fold-case
35
36 (define-module rfc.tls
37 (use gauche.vport)
38 (use gauche.connection)
39 (use gauche.net)
40 (use util.match)
41 (export <tls> make-tls tls-connect
42 tls-bind tls-accept tls-poll tls-close
43 tls-load-certificate tls-load-private-key
44 tls-read tls-write
45 tls-input-port tls-output-port
46 tls-ca-bundle-path tls-debug-level-set!
47 default-tls-class
48
49 ;; connection interface
50 connection-self-address connection-peer-address
51 connection-input-port connection-output-port
52 connection-shutdown connection-close
53
54 ;; deprecated interface
55 SSL_SERVER_VERIFY_LATER SSL_CLIENT_AUTHENTICATION
56 SSL_DISPLAY_BYTES SSL_DISPLAY_STATES SSL_DISPLAY_CERTS
57 SSL_DISPLAY_RSA SSL_CONNECT_IN_PARTS SSL_NO_DEFAULT_KEY
58 SSL_OBJ_X509_CERT SSL_OBJ_X509_CACERT SSL_OBJ_RSA_KEY
59 SSL_OBJ_PKCS8 SSL_OBJ_PKCS12
60 tls-load-object tls-destroy
61 )
62 )
63 (select-module rfc.tls)
64
65 ;; The initialization of default-tls-class depends on the availability
66 ;; of classes and tls-ca-bundle-path.
67
68 (without-precompiling
69 (cond-expand
70 [gauche.net.tls.mbedtls
71 ;; Set rfc.tls.mbed to be autoladed when <mbed-tls> is used.
72 (autoload rfc.tls.mbed <mbed-tls>)
73 (export <mbed-tls>)
74 (when (eq? (tls-ca-bundle-path) 'check)
75 (if (%tls-system-ca-bundle-available?)
76 (tls-ca-bundle-path 'system)
77 (tls-ca-bundle-path #f)))
78 (default-tls-class (delay <mbed-tls>))
79 ]
80 [else
81 (when (eq? (tls-ca-bundle-path) 'check)
82 (tls-ca-bundle-path #f))
83 (default-tls-class #f)]))
84
85 (inline-stub
86 (declcode
87 (.include "gauche-tls.h"))
88
89 (declare-stub-type <tls> "ScmTLS*")
90
91 (define-cproc make-tls (:rest initargs) Scm_MakeTLS)
92 (define-cproc tls-load-certificate (tls::<tls> filename::<const-cstring>)
93 Scm_TLSLoadCertificate)
94 (define-cproc tls-load-private-key (tls::<tls>
95 filename::<const-cstring>
96 password::<const-cstring>?)
97 Scm_TLSLoadPrivateKey)
98 (define-cproc %tls-connect (tls::<tls>
99 host::<const-cstring>
100 port::<const-cstring>
101 proto)
102 Scm_TLSConnect)
103 (define-cproc %tls-bind (tls::<tls>
104 ip::<const-cstring>?
105 port::<const-cstring>
106 proto)
107 Scm_TLSBind)
108 (define-cproc %tls-accept (tls::<tls>) Scm_TLSAccept)
109 (define-cproc %tls-close (tls::<tls>) Scm_TLSClose)
110 (define-cproc tls-read (tls::<tls>) Scm_TLSRead)
111 (define-cproc tls-write (tls::<tls> msg) Scm_TLSWrite)
112 (define-cproc tls-input-port (tls::<tls>) Scm_TLSInputPort)
113 (define-cproc tls-output-port (tls::<tls>) Scm_TLSOutputPort)
114 (define-cproc tls-poll (tls::<tls> rwflags::<list> :optional (timeout #f))
115 (let* ([ts::ScmTimeSpec]
116 [pts::ScmTimeSpec* (Scm_GetTimeSpec timeout (& ts))]
117 [iflags::u_long 0])
118 (for-each (lambda (f)
119 (cond
120 [(SCM_EQ f 'read) (logior= iflags TLS_POLL_READ)]
121 [(SCM_EQ f 'write) (logior= iflags TLS_POLL_WRITE)]
122 [else
123 (Scm_Error "List of 'read and 'write expected, but got: %S"
124 rwflags)]))
125 rwflags)
126 (let* ([r::u_long (Scm_TLSPoll tls iflags pts)]
127 [result SCM_NIL])
128 (when (logand r TLS_POLL_READ)
129 (set! result (Scm_Cons 'read result)))
130 (when (logand r TLS_POLL_WRITE)
131 (set! result (Scm_Cons 'write result)))
132 (return result))))
133 (define-cproc tls-debug-level-set! (level::<int>) ::<void>
134 (Scm_TLSSetDebugLevel level))
135 ;; internal routine to regiseter debug level setter
136 (define-cproc %tls-register-debug-level-callback (setter) ::<void>
137 (Scm_TLSRegisterDebugLevelCallback setter))
138
139 ;; internal
140 (define-cproc %tls-input-port-set! (tls::<tls> port) Scm_TLSInputPortSet)
141 (define-cproc %tls-output-port-set! (tls::<tls> port) Scm_TLSOutputPortSet)
142 (define-cproc %tls-get-self-address (tls::<tls>)
143 (return (Scm_TLSGetConnectionAddress tls TLS_SELF_ADDRESS)))
144 (define-cproc %tls-get-peer-address (tls::<tls>)
145 (return (Scm_TLSGetConnectionAddress tls TLS_PEER_ADDRESS)))
146
147 ;; DEPRECATED APIs
148 (define-enum SSL_SERVER_VERIFY_LATER)
149 (define-enum SSL_CLIENT_AUTHENTICATION)
150 (define-enum SSL_NO_DEFAULT_KEY)
151 (define-enum SSL_DISPLAY_BYTES)
152 (define-enum SSL_DISPLAY_STATES)
153 (define-enum SSL_DISPLAY_CERTS)
154 (define-enum SSL_DISPLAY_RSA)
155 (define-enum SSL_CONNECT_IN_PARTS)
156 (define-enum SSL_OBJ_X509_CERT)
157 (define-enum SSL_OBJ_X509_CACERT)
158 (define-enum SSL_OBJ_RSA_KEY)
159 (define-enum SSL_OBJ_PKCS8)
160 (define-enum SSL_OBJ_PKCS12)
161 (define-cproc tls-load-object (tls::<tls>
162 obj-type::<fixnum>
163 filename::<const-cstring>
164 :optional (password::<const-cstring>? #f))
165 ::<void>
166 (Scm_Warn "tls-load-object is deprecated. Use tls-load-certificate or \
167 tls-load-private-key.\n")
168 (case obj-type
169 [(SSL_OBJ_X509_CERT SSL_OBJ_X509_CACERT)
170 (Scm_TLSLoadCertificate tls filename)]
171 [(SSL_OBJ_RSA_KEY SSL_OBJ_PKCS8 SSL_OBJ_PKCS12)
172 (Scm_TLSLoadPrivateKey tls filename password)]
173 [else (Scm_Error "Invalid obj-type: %d" obj-type)]))
174
175 (declcode "void Scm_Init_tls(ScmModule *);")
176 (initcode "Scm_Init_tls(Scm_CurrentModule());")
177 )
178
179 ;; API
180 (define (tls-connect tls host port :optional (proto 'tcp))
181 (assume-type port (</> <string> <integer>))
182 (let1 p (if (integer? port)
183 (if (or (inexact? port) (negative? port))
184 (error "Nonnegative exact integer or string expected, but got:"
185 port)
186 (x->string port))
187 port)
188 (%tls-connect tls host p proto)
189 (%tls-input-port-set! tls (make-tls-input-port tls))
190 (%tls-output-port-set! tls (make-tls-output-port tls)))
191 tls)
192
193 ;; API
194 (define (tls-bind tls host port :optional (proto 'tcp))
195 (assume-type port (</> <string> <integer>))
196 (let1 p (if (integer? port)
197 (if (or (inexact? port) (negative? port))
198 (error "Nonnegative exact integer or string expected, but got:"
199 port)
200 (x->string port))
201 port)
202 (%tls-bind tls host p proto)))
203
204 ;; API
205 (define (tls-accept tls)
206 (rlet1 new-tls (%tls-accept tls)
207 (%tls-input-port-set! new-tls (make-tls-input-port new-tls))
208 (%tls-output-port-set! new-tls (make-tls-output-port new-tls))))
209
210 ;; API
211 (define (tls-close t)
212 (when (input-port? (tls-input-port t))
213 (close-input-port (tls-input-port t)))
214 (when (output-port? (tls-output-port t))
215 (close-output-port (tls-output-port t)))
216 (%tls-close t))
217
218 ;; Internal
219 (define-cproc %tls-system-ca-bundle-available? () ::<boolean>
220 Scm_TLSSystemCABundleAvailable)
221
222 (define (make-tls-input-port tls)
223 (rlet1 ip (make <virtual-input-port>)
224 (set! (~ ip'getb)
225 (let ((buf #f) (pos 0) (size 0))
226 (rec (reader)
227 (if buf
228 (rlet1 r (string-byte-ref buf pos)
229 (set! pos (+ pos 1))
230 (when (= pos size) (set! buf #f)))
231 (let1 data (tls-read tls)
232 (if (eof-object? data)
233 data
234 (begin
235 (set! buf data)
236 (set! size (string-size buf))
237 (set! pos 0)
238 (reader))))))))))
239
240 (define (make-tls-output-port tls)
241 (rlet1 op (make <virtual-output-port>)
242 (set! (~ op'puts) (^[msg] (tls-write tls msg)))
243 (set! (~ op'putb) (^[b] (tls-write tls (make-byte-string 1 b))))))
244
245 ;; Deprecated
246 (define (tls-destroy tls) (tls-close tls))
247
248 (define-method connection-self-address ((s <tls>)) (%tls-get-self-address s))
249 (define-method connection-peer-address ((s <tls>)) (%tls-get-peer-address s))
250 (define-method connection-input-port ((s <tls>))
251 (tls-input-port s))
252 (define-method connection-output-port ((s <tls>))
253 (tls-output-port s))
254 (define-method connection-shutdown ((s <tls>) how)
255 ;; for now, we shutdown connection entirely regardless of HOW argument.
256 (tls-close s))
257 (define-method connection-close ((s <tls>))
258 (tls-destroy s))