r7747: - simplified the ldap server buffer handling
[samba.git] / source4 / lib / tls / tls.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    transport layer security handling code
5
6    Copyright (C) Andrew Tridgell 2005
7    
8    This program 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 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "lib/events/events.h"
25 #include "lib/socket/socket.h"
26 #include "lib/tls/tls.h"
27
28 #if HAVE_LIBGNUTLS
29 #include "gnutls/gnutls.h"
30
31 #define DH_BITS 1024
32
33 /* hold persistent tls data */
34 struct tls_params {
35         gnutls_certificate_credentials x509_cred;
36         gnutls_dh_params dh_params;
37         BOOL tls_enabled;
38 };
39
40 /* hold per connection tls data */
41 struct tls_context {
42         struct tls_params *params;
43         struct socket_context *socket;
44         struct fd_event *fde;
45         gnutls_session session;
46         BOOL done_handshake;
47         BOOL have_first_byte;
48         uint8_t first_byte;
49         BOOL tls_enabled;
50         BOOL tls_detect;
51         const char *plain_chars;
52         BOOL output_pending;
53 };
54
55
56 /*
57   callback for reading from a socket
58 */
59 static ssize_t tls_pull(gnutls_transport_ptr ptr, void *buf, size_t size)
60 {
61         struct tls_context *tls = talloc_get_type(ptr, struct tls_context);
62         NTSTATUS status;
63         size_t nread;
64         
65         if (tls->have_first_byte) {
66                 *(uint8_t *)buf = tls->first_byte;
67                 tls->have_first_byte = False;
68                 return 1;
69         }
70
71         status = socket_recv(tls->socket, buf, size, &nread, 0);
72         if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
73                 return 0;
74         }
75         if (NT_STATUS_IS_ERR(status)) {
76                 EVENT_FD_NOT_READABLE(tls->fde);
77                 EVENT_FD_NOT_WRITEABLE(tls->fde);
78                 errno = EBADF;
79                 return -1;
80         }
81         if (!NT_STATUS_IS_OK(status)) {
82                 EVENT_FD_READABLE(tls->fde);
83                 EVENT_FD_NOT_WRITEABLE(tls->fde);
84                 errno = EAGAIN;
85                 return -1;
86         }
87         if (tls->output_pending) {
88                 EVENT_FD_WRITEABLE(tls->fde);
89         }
90         if (size != nread) {
91                 EVENT_FD_READABLE(tls->fde);
92         }
93         return nread;
94 }
95
96 /*
97   callback for writing to a socket
98 */
99 static ssize_t tls_push(gnutls_transport_ptr ptr, const void *buf, size_t size)
100 {
101         struct tls_context *tls = talloc_get_type(ptr, struct tls_context);
102         NTSTATUS status;
103         size_t nwritten;
104         DATA_BLOB b;
105
106         if (!tls->tls_enabled) {
107                 return size;
108         }
109
110         b.data = discard_const(buf);
111         b.length = size;
112
113         status = socket_send(tls->socket, &b, &nwritten, 0);
114         if (!NT_STATUS_IS_OK(status)) {
115                 EVENT_FD_WRITEABLE(tls->fde);
116                 return -1;
117         }
118         if (size != nwritten) {
119                 EVENT_FD_WRITEABLE(tls->fde);
120         }
121         return nwritten;
122 }
123
124 /*
125   destroy a tls session
126  */
127 static int tls_destructor(void *ptr)
128 {
129         struct tls_context *tls = talloc_get_type(ptr, struct tls_context);
130         int ret;
131         ret = gnutls_bye(tls->session, GNUTLS_SHUT_WR);
132         if (ret < 0) {
133                 DEBUG(0,("TLS gnutls_bye failed - %s\n", gnutls_strerror(ret)));
134         }
135         return 0;
136 }
137
138
139 /*
140   possibly continue the handshake process
141 */
142 static NTSTATUS tls_handshake(struct tls_context *tls)
143 {
144         int ret;
145
146         if (tls->done_handshake) {
147                 return NT_STATUS_OK;
148         }
149         
150         ret = gnutls_handshake(tls->session);
151         if (ret == GNUTLS_E_INTERRUPTED || ret == GNUTLS_E_AGAIN) {
152                 return STATUS_MORE_ENTRIES;
153         }
154         if (ret < 0) {
155                 DEBUG(0,("TLS gnutls_handshake failed - %s\n", gnutls_strerror(ret)));
156                 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
157         }
158         tls->done_handshake = True;
159         return NT_STATUS_OK;
160 }
161
162 /*
163   see how many bytes are pending on the connection
164 */
165 NTSTATUS tls_socket_pending(struct tls_context *tls, size_t *npending)
166 {
167         if (!tls->tls_enabled || tls->tls_detect) {
168                 return socket_pending(tls->socket, npending);
169         }
170         *npending = gnutls_record_check_pending(tls->session);
171         if (*npending == 0) {
172                 return socket_pending(tls->socket, npending);
173         }
174         return NT_STATUS_OK;
175 }
176
177 /*
178   receive data either by tls or normal socket_recv
179 */
180 NTSTATUS tls_socket_recv(struct tls_context *tls, void *buf, size_t wantlen, 
181                          size_t *nread)
182 {
183         int ret;
184         NTSTATUS status;
185         if (tls->tls_enabled && tls->tls_detect) {
186                 status = socket_recv(tls->socket, &tls->first_byte, 1, nread, 0);
187                 NT_STATUS_NOT_OK_RETURN(status);
188                 if (*nread == 0) return NT_STATUS_OK;
189                 tls->tls_detect = False;
190                 /* look for the first byte of a valid HTTP operation */
191                 if (strchr(tls->plain_chars, tls->first_byte)) {
192                         /* not a tls link */
193                         tls->tls_enabled = False;
194                         *(uint8_t *)buf = tls->first_byte;
195                         return NT_STATUS_OK;
196                 }
197                 tls->have_first_byte = True;
198         }
199
200         if (!tls->tls_enabled) {
201                 return socket_recv(tls->socket, buf, wantlen, nread, 0);
202         }
203
204         status = tls_handshake(tls);
205         NT_STATUS_NOT_OK_RETURN(status);
206
207         ret = gnutls_record_recv(tls->session, buf, wantlen);
208         if (ret == GNUTLS_E_INTERRUPTED || ret == GNUTLS_E_AGAIN) {
209                 return STATUS_MORE_ENTRIES;
210         }
211         if (ret < 0) {
212                 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
213         }
214         *nread = ret;
215         return NT_STATUS_OK;
216 }
217
218
219 /*
220   send data either by tls or normal socket_recv
221 */
222 NTSTATUS tls_socket_send(struct tls_context *tls, const DATA_BLOB *blob, size_t *sendlen)
223 {
224         NTSTATUS status;
225         int ret;
226
227         if (!tls->tls_enabled) {
228                 return socket_send(tls->socket, blob, sendlen, 0);
229         }
230
231         status = tls_handshake(tls);
232         NT_STATUS_NOT_OK_RETURN(status);
233
234         ret = gnutls_record_send(tls->session, blob->data, blob->length);
235         if (ret == GNUTLS_E_INTERRUPTED || ret == GNUTLS_E_AGAIN) {
236                 return STATUS_MORE_ENTRIES;
237         }
238         if (ret < 0) {
239                 DEBUG(0,("gnutls_record_send of %d failed - %s\n", blob->length, gnutls_strerror(ret)));
240                 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
241         }
242         *sendlen = ret;
243         tls->output_pending = (ret < blob->length);
244         return NT_STATUS_OK;
245 }
246
247
248 /*
249   initialise global tls state
250 */
251 struct tls_params *tls_initialise(TALLOC_CTX *mem_ctx)
252 {
253         struct tls_params *params;
254         int ret;
255         const char *keyfile = lp_tls_keyfile();
256         const char *certfile = lp_tls_certfile();
257         const char *cafile = lp_tls_cafile();
258         const char *crlfile = lp_tls_crlfile();
259         void tls_cert_generate(TALLOC_CTX *, const char *, const char *, const char *);
260
261         params = talloc(mem_ctx, struct tls_params);
262         if (params == NULL) return NULL;
263
264         if (!lp_tls_enabled() || keyfile == NULL || *keyfile == 0) {
265                 params->tls_enabled = False;
266                 return params;
267         }
268
269         if (!file_exist(cafile)) {
270                 tls_cert_generate(params, keyfile, certfile, cafile);
271         }
272
273         ret = gnutls_global_init();
274         if (ret < 0) goto init_failed;
275
276         gnutls_certificate_allocate_credentials(&params->x509_cred);
277         if (ret < 0) goto init_failed;
278
279         if (cafile && *cafile) {
280                 ret = gnutls_certificate_set_x509_trust_file(params->x509_cred, cafile, 
281                                                              GNUTLS_X509_FMT_PEM);      
282                 if (ret < 0) {
283                         DEBUG(0,("TLS failed to initialise cafile %s\n", cafile));
284                         goto init_failed;
285                 }
286         }
287
288         if (crlfile && *crlfile) {
289                 ret = gnutls_certificate_set_x509_crl_file(params->x509_cred, 
290                                                            crlfile, 
291                                                            GNUTLS_X509_FMT_PEM);
292                 if (ret < 0) {
293                         DEBUG(0,("TLS failed to initialise crlfile %s\n", crlfile));
294                         goto init_failed;
295                 }
296         }
297         
298         ret = gnutls_certificate_set_x509_key_file(params->x509_cred, 
299                                                    certfile, keyfile,
300                                                    GNUTLS_X509_FMT_PEM);
301         if (ret < 0) {
302                 DEBUG(0,("TLS failed to initialise certfile %s and keyfile %s\n", 
303                          certfile, keyfile));
304                 goto init_failed;
305         }
306         
307         ret = gnutls_dh_params_init(&params->dh_params);
308         if (ret < 0) goto init_failed;
309
310         ret = gnutls_dh_params_generate2(params->dh_params, DH_BITS);
311         if (ret < 0) goto init_failed;
312
313         gnutls_certificate_set_dh_params(params->x509_cred, params->dh_params);
314
315         params->tls_enabled = True;
316         return params;
317
318 init_failed:
319         DEBUG(0,("GNUTLS failed to initialise - %s\n", gnutls_strerror(ret)));
320         params->tls_enabled = False;
321         return params;
322 }
323
324
325 /*
326   setup for a new connection
327 */
328 struct tls_context *tls_init_server(struct tls_params *params, 
329                                     struct socket_context *socket,
330                                     struct fd_event *fde, 
331                                     const char *plain_chars)
332 {
333         struct tls_context *tls;
334         int ret;
335
336         tls = talloc(socket, struct tls_context);
337         if (tls == NULL) return NULL;
338
339         tls->socket          = socket;
340         tls->fde             = fde;
341
342         if (!params->tls_enabled) {
343                 tls->tls_enabled = False;
344                 return tls;
345         }
346
347 #define TLSCHECK(call) do { \
348         ret = call; \
349         if (ret < 0) { \
350                 DEBUG(0,("TLS %s - %s\n", #call, gnutls_strerror(ret))); \
351                 goto failed; \
352         } \
353 } while (0)
354
355         TLSCHECK(gnutls_init(&tls->session, GNUTLS_SERVER));
356
357         talloc_set_destructor(tls, tls_destructor);
358
359         TLSCHECK(gnutls_set_default_priority(tls->session));
360         TLSCHECK(gnutls_credentials_set(tls->session, GNUTLS_CRD_CERTIFICATE, 
361                                         params->x509_cred));
362         gnutls_certificate_server_set_request(tls->session, GNUTLS_CERT_REQUEST);
363         gnutls_dh_set_prime_bits(tls->session, DH_BITS);
364         gnutls_transport_set_ptr(tls->session, (gnutls_transport_ptr)tls);
365         gnutls_transport_set_pull_function(tls->session, (gnutls_pull_func)tls_pull);
366         gnutls_transport_set_push_function(tls->session, (gnutls_push_func)tls_push);
367         gnutls_transport_set_lowat(tls->session, 0);
368
369         tls->plain_chars = plain_chars;
370         if (plain_chars) {
371                 tls->tls_detect = True;
372         } else {
373                 tls->tls_detect = False;
374         }
375
376         tls->output_pending  = False;
377         tls->params          = params;
378         tls->done_handshake  = False;
379         tls->have_first_byte = False;
380         tls->tls_enabled     = True;
381         
382         return tls;
383
384 failed:
385         DEBUG(0,("TLS init connection failed - %s\n", gnutls_strerror(ret)));
386         tls->tls_enabled = False;
387         params->tls_enabled = False;
388         return tls;
389 }
390
391 BOOL tls_enabled(struct tls_context *tls)
392 {
393         return tls->tls_enabled;
394 }
395
396 BOOL tls_support(struct tls_params *params)
397 {
398         return params->tls_enabled;
399 }
400
401
402 #else
403
404 /* for systems without tls we just map the tls socket calls to the
405    normal socket calls */
406
407 struct tls_params *tls_initialise(TALLOC_CTX *mem_ctx)
408 {
409         return talloc_new(mem_ctx);
410 }
411
412 struct tls_context *tls_init_server(struct tls_params *params, 
413                                     struct socket_context *sock, 
414                                     struct fd_event *fde,
415                                     const char *plain_chars)
416 {
417         if (plain_chars == NULL) return NULL;
418         return (struct tls_context *)sock;
419 }
420
421
422 NTSTATUS tls_socket_recv(struct tls_context *tls, void *buf, size_t wantlen, 
423                          size_t *nread)
424 {
425         return socket_recv((struct socket_context *)tls, buf, wantlen, nread, 0);
426 }
427
428 NTSTATUS tls_socket_send(struct tls_context *tls, const DATA_BLOB *blob, size_t *sendlen)
429 {
430         return socket_send((struct socket_context *)tls, blob, sendlen, 0);
431 }
432
433 BOOL tls_enabled(struct tls_context *tls)
434 {
435         return False;
436 }
437
438 BOOL tls_support(struct tls_params *params)
439 {
440         return False;
441 }
442
443 NTSTATUS tls_socket_pending(struct tls_context *tls, size_t *npending)
444 {
445         return socket_pending((struct socket_context *)tls, npending);
446 }
447
448 #endif