2 Unix SMB/CIFS implementation.
4 transport layer security handling code
6 Copyright (C) Andrew Tridgell 2004-2005
7 Copyright (C) Stefan Metzmacher 2004
8 Copyright (C) Andrew Bartlett 2006
10 This program 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 3 of the License, or
13 (at your option) any later version.
15 This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "system/filesys.h"
26 #include "lib/events/events.h"
27 #include "lib/socket/socket.h"
28 #include "lib/tls/tls.h"
29 #include "param/param.h"
32 #include <gnutls/gnutls.h>
36 #if defined(HAVE_GNUTLS_DATUM) && !defined(HAVE_GNUTLS_DATUM_T)
37 typedef gnutls_datum gnutls_datum_t
;
40 /* hold persistent tls data */
42 gnutls_certificate_credentials x509_cred
;
43 gnutls_dh_params dh_params
;
48 /* hold per connection tls data */
50 struct socket_context
*socket
;
51 struct tevent_fd
*fde
;
54 gnutls_session session
;
59 const char *plain_chars
;
61 gnutls_certificate_credentials xcred
;
66 bool tls_enabled(struct socket_context
*sock
)
68 struct tls_context
*tls
;
72 if (strcmp(sock
->backend_name
, "tls") != 0) {
75 tls
= talloc_get_type(sock
->private_data
, struct tls_context
);
79 return tls
->tls_enabled
;
85 static const struct socket_ops tls_socket_ops
;
87 static NTSTATUS
tls_socket_init(struct socket_context
*sock
)
90 case SOCKET_TYPE_STREAM
:
93 return NT_STATUS_INVALID_PARAMETER
;
96 sock
->backend_name
= "tls";
101 #define TLSCHECK(call) do { \
104 DEBUG(0,("TLS %s - %s\n", #call, gnutls_strerror(ret))); \
111 callback for reading from a socket
113 static ssize_t
tls_pull(gnutls_transport_ptr ptr
, void *buf
, size_t size
)
115 struct tls_context
*tls
= talloc_get_type(ptr
, struct tls_context
);
119 if (tls
->have_first_byte
) {
120 *(uint8_t *)buf
= tls
->first_byte
;
121 tls
->have_first_byte
= false;
125 status
= socket_recv(tls
->socket
, buf
, size
, &nread
);
126 if (NT_STATUS_EQUAL(status
, NT_STATUS_END_OF_FILE
)) {
129 if (NT_STATUS_IS_ERR(status
)) {
130 TEVENT_FD_NOT_READABLE(tls
->fde
);
131 TEVENT_FD_NOT_WRITEABLE(tls
->fde
);
135 if (!NT_STATUS_IS_OK(status
)) {
136 TEVENT_FD_READABLE(tls
->fde
);
140 if (tls
->output_pending
) {
141 TEVENT_FD_WRITEABLE(tls
->fde
);
144 TEVENT_FD_READABLE(tls
->fde
);
150 callback for writing to a socket
152 static ssize_t
tls_push(gnutls_transport_ptr ptr
, const void *buf
, size_t size
)
154 struct tls_context
*tls
= talloc_get_type(ptr
, struct tls_context
);
156 size_t nwritten
, total_nwritten
= 0;
159 if (!tls
->tls_enabled
) {
163 b
.data
= discard_const(buf
);
166 /* Cope with socket_wrapper 1500 byte chunking for PCAP */
168 status
= socket_send(tls
->socket
, &b
, &nwritten
);
170 if (NT_STATUS_EQUAL(status
, STATUS_MORE_ENTRIES
)) {
174 if (!NT_STATUS_IS_OK(status
)) {
175 TEVENT_FD_WRITEABLE(tls
->fde
);
179 total_nwritten
+= nwritten
;
181 if (size
== nwritten
) {
186 b
.length
-= nwritten
;
188 TEVENT_FD_WRITEABLE(tls
->fde
);
191 return total_nwritten
;
195 destroy a tls session
197 static int tls_destructor(struct tls_context
*tls
)
200 ret
= gnutls_bye(tls
->session
, GNUTLS_SHUT_WR
);
202 DEBUG(4,("TLS gnutls_bye failed - %s\n", gnutls_strerror(ret
)));
209 possibly continue the handshake process
211 static NTSTATUS
tls_handshake(struct tls_context
*tls
)
215 if (tls
->done_handshake
) {
219 ret
= gnutls_handshake(tls
->session
);
220 if (ret
== GNUTLS_E_INTERRUPTED
|| ret
== GNUTLS_E_AGAIN
) {
221 if (gnutls_record_get_direction(tls
->session
) == 1) {
222 TEVENT_FD_WRITEABLE(tls
->fde
);
224 return STATUS_MORE_ENTRIES
;
227 DEBUG(0,("TLS gnutls_handshake failed - %s\n", gnutls_strerror(ret
)));
228 return NT_STATUS_UNEXPECTED_NETWORK_ERROR
;
230 tls
->done_handshake
= true;
235 possibly continue an interrupted operation
237 static NTSTATUS
tls_interrupted(struct tls_context
*tls
)
241 if (!tls
->interrupted
) {
244 if (gnutls_record_get_direction(tls
->session
) == 1) {
245 ret
= gnutls_record_send(tls
->session
, NULL
, 0);
247 ret
= gnutls_record_recv(tls
->session
, NULL
, 0);
249 if (ret
== GNUTLS_E_INTERRUPTED
|| ret
== GNUTLS_E_AGAIN
) {
250 return STATUS_MORE_ENTRIES
;
252 tls
->interrupted
= false;
257 see how many bytes are pending on the connection
259 static NTSTATUS
tls_socket_pending(struct socket_context
*sock
, size_t *npending
)
261 struct tls_context
*tls
= talloc_get_type(sock
->private_data
, struct tls_context
);
262 if (!tls
->tls_enabled
|| tls
->tls_detect
) {
263 return socket_pending(tls
->socket
, npending
);
265 *npending
= gnutls_record_check_pending(tls
->session
);
266 if (*npending
== 0) {
267 NTSTATUS status
= socket_pending(tls
->socket
, npending
);
268 if (*npending
== 0) {
269 /* seems to be a gnutls bug */
278 receive data either by tls or normal socket_recv
280 static NTSTATUS
tls_socket_recv(struct socket_context
*sock
, void *buf
,
281 size_t wantlen
, size_t *nread
)
285 struct tls_context
*tls
= talloc_get_type(sock
->private_data
, struct tls_context
);
287 if (tls
->tls_enabled
&& tls
->tls_detect
) {
288 status
= socket_recv(tls
->socket
, &tls
->first_byte
, 1, nread
);
289 NT_STATUS_NOT_OK_RETURN(status
);
290 if (*nread
== 0) return NT_STATUS_OK
;
291 tls
->tls_detect
= false;
292 /* look for the first byte of a valid HTTP operation */
293 if (strchr(tls
->plain_chars
, tls
->first_byte
)) {
295 tls
->tls_enabled
= false;
296 *(uint8_t *)buf
= tls
->first_byte
;
299 tls
->have_first_byte
= true;
302 if (!tls
->tls_enabled
) {
303 return socket_recv(tls
->socket
, buf
, wantlen
, nread
);
306 status
= tls_handshake(tls
);
307 NT_STATUS_NOT_OK_RETURN(status
);
309 status
= tls_interrupted(tls
);
310 NT_STATUS_NOT_OK_RETURN(status
);
312 ret
= gnutls_record_recv(tls
->session
, buf
, wantlen
);
313 if (ret
== GNUTLS_E_INTERRUPTED
|| ret
== GNUTLS_E_AGAIN
) {
314 if (gnutls_record_get_direction(tls
->session
) == 1) {
315 TEVENT_FD_WRITEABLE(tls
->fde
);
317 tls
->interrupted
= true;
318 return STATUS_MORE_ENTRIES
;
321 return NT_STATUS_UNEXPECTED_NETWORK_ERROR
;
329 send data either by tls or normal socket_recv
331 static NTSTATUS
tls_socket_send(struct socket_context
*sock
,
332 const DATA_BLOB
*blob
, size_t *sendlen
)
336 struct tls_context
*tls
= talloc_get_type(sock
->private_data
, struct tls_context
);
338 if (!tls
->tls_enabled
) {
339 return socket_send(tls
->socket
, blob
, sendlen
);
342 status
= tls_handshake(tls
);
343 NT_STATUS_NOT_OK_RETURN(status
);
345 status
= tls_interrupted(tls
);
346 NT_STATUS_NOT_OK_RETURN(status
);
348 ret
= gnutls_record_send(tls
->session
, blob
->data
, blob
->length
);
349 if (ret
== GNUTLS_E_INTERRUPTED
|| ret
== GNUTLS_E_AGAIN
) {
350 if (gnutls_record_get_direction(tls
->session
) == 1) {
351 TEVENT_FD_WRITEABLE(tls
->fde
);
353 tls
->interrupted
= true;
354 return STATUS_MORE_ENTRIES
;
357 DEBUG(0,("gnutls_record_send of %d failed - %s\n", (int)blob
->length
, gnutls_strerror(ret
)));
358 return NT_STATUS_UNEXPECTED_NETWORK_ERROR
;
361 tls
->output_pending
= (ret
< blob
->length
);
367 initialise global tls state
369 struct tls_params
*tls_initialise(TALLOC_CTX
*mem_ctx
, struct loadparm_context
*lp_ctx
)
371 struct tls_params
*params
;
374 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
375 const char *keyfile
= lpcfg_tls_keyfile(tmp_ctx
, lp_ctx
);
376 const char *certfile
= lpcfg_tls_certfile(tmp_ctx
, lp_ctx
);
377 const char *cafile
= lpcfg_tls_cafile(tmp_ctx
, lp_ctx
);
378 const char *crlfile
= lpcfg_tls_crlfile(tmp_ctx
, lp_ctx
);
379 const char *dhpfile
= lpcfg_tls_dhpfile(tmp_ctx
, lp_ctx
);
380 void tls_cert_generate(TALLOC_CTX
*, const char *, const char *, const char *, const char *);
381 params
= talloc(mem_ctx
, struct tls_params
);
382 if (params
== NULL
) {
383 talloc_free(tmp_ctx
);
387 if (!lpcfg_tls_enabled(lp_ctx
) || keyfile
== NULL
|| *keyfile
== 0) {
388 params
->tls_enabled
= false;
389 talloc_free(tmp_ctx
);
393 if (!file_exist(cafile
)) {
394 char *hostname
= talloc_asprintf(mem_ctx
, "%s.%s",
395 lpcfg_netbios_name(lp_ctx
),
396 lpcfg_dnsdomain(lp_ctx
));
397 if (hostname
== NULL
) {
398 ret
= GNUTLS_E_MEMORY_ERROR
;
401 tls_cert_generate(params
, hostname
, keyfile
, certfile
, cafile
);
402 talloc_free(hostname
);
405 if (file_exist(keyfile
) &&
406 !file_check_permissions(keyfile
, geteuid(), 0600, &st
))
408 DEBUG(0, ("Invalid permissions on TLS private key file '%s':\n"
409 "owner uid %u should be %u, mode 0%o should be 0%o\n"
410 "This is known as CVE-2013-4476.\n"
411 "Removing all tls .pem files will cause an "
412 "auto-regeneration with the correct permissions.\n",
414 (unsigned int)st
.st_uid
, geteuid(),
415 (unsigned int)(st
.st_mode
& 0777), 0600));
416 talloc_free(tmp_ctx
);
420 ret
= gnutls_global_init();
421 if (ret
< 0) goto init_failed
;
423 gnutls_certificate_allocate_credentials(¶ms
->x509_cred
);
424 if (ret
< 0) goto init_failed
;
426 if (cafile
&& *cafile
) {
427 ret
= gnutls_certificate_set_x509_trust_file(params
->x509_cred
, cafile
,
428 GNUTLS_X509_FMT_PEM
);
430 DEBUG(0,("TLS failed to initialise cafile %s\n", cafile
));
435 if (crlfile
&& *crlfile
) {
436 ret
= gnutls_certificate_set_x509_crl_file(params
->x509_cred
,
438 GNUTLS_X509_FMT_PEM
);
440 DEBUG(0,("TLS failed to initialise crlfile %s\n", crlfile
));
445 ret
= gnutls_certificate_set_x509_key_file(params
->x509_cred
,
447 GNUTLS_X509_FMT_PEM
);
449 DEBUG(0,("TLS failed to initialise certfile %s and keyfile %s\n",
454 ret
= gnutls_dh_params_init(¶ms
->dh_params
);
455 if (ret
< 0) goto init_failed
;
457 if (dhpfile
&& *dhpfile
) {
458 gnutls_datum_t dhparms
;
460 dhparms
.data
= (uint8_t *)file_load(dhpfile
, &size
, 0, mem_ctx
);
463 DEBUG(0,("Failed to read DH Parms from %s\n", dhpfile
));
468 ret
= gnutls_dh_params_import_pkcs3(params
->dh_params
, &dhparms
, GNUTLS_X509_FMT_PEM
);
469 if (ret
< 0) goto init_failed
;
471 ret
= gnutls_dh_params_generate2(params
->dh_params
, DH_BITS
);
472 if (ret
< 0) goto init_failed
;
475 gnutls_certificate_set_dh_params(params
->x509_cred
, params
->dh_params
);
477 params
->tls_enabled
= true;
479 talloc_free(tmp_ctx
);
483 DEBUG(0,("GNUTLS failed to initialise - %s\n", gnutls_strerror(ret
)));
484 params
->tls_enabled
= false;
485 talloc_free(tmp_ctx
);
491 setup for a new connection
493 struct socket_context
*tls_init_server(struct tls_params
*params
,
494 struct socket_context
*socket_ctx
,
495 struct tevent_fd
*fde
,
496 const char *plain_chars
)
498 struct tls_context
*tls
;
500 struct socket_context
*new_sock
;
503 nt_status
= socket_create_with_ops(socket_ctx
, &tls_socket_ops
, &new_sock
,
505 socket_ctx
->flags
| SOCKET_FLAG_ENCRYPT
);
506 if (!NT_STATUS_IS_OK(nt_status
)) {
510 tls
= talloc(new_sock
, struct tls_context
);
515 tls
->socket
= socket_ctx
;
516 talloc_steal(tls
, socket_ctx
);
519 new_sock
->private_data
= tls
;
521 if (!params
->tls_enabled
) {
522 talloc_free(new_sock
);
526 TLSCHECK(gnutls_init(&tls
->session
, GNUTLS_SERVER
));
528 talloc_set_destructor(tls
, tls_destructor
);
530 TLSCHECK(gnutls_set_default_priority(tls
->session
));
531 TLSCHECK(gnutls_credentials_set(tls
->session
, GNUTLS_CRD_CERTIFICATE
,
533 gnutls_certificate_server_set_request(tls
->session
, GNUTLS_CERT_REQUEST
);
534 gnutls_dh_set_prime_bits(tls
->session
, DH_BITS
);
535 gnutls_transport_set_ptr(tls
->session
, (gnutls_transport_ptr
)tls
);
536 gnutls_transport_set_pull_function(tls
->session
, (gnutls_pull_func
)tls_pull
);
537 gnutls_transport_set_push_function(tls
->session
, (gnutls_push_func
)tls_push
);
538 #if GNUTLS_VERSION_MAJOR < 3
539 gnutls_transport_set_lowat(tls
->session
, 0);
542 tls
->plain_chars
= plain_chars
;
544 tls
->tls_detect
= true;
546 tls
->tls_detect
= false;
549 tls
->output_pending
= false;
550 tls
->done_handshake
= false;
551 tls
->have_first_byte
= false;
552 tls
->tls_enabled
= true;
553 tls
->interrupted
= false;
555 new_sock
->state
= SOCKET_STATE_SERVER_CONNECTED
;
560 DEBUG(0,("TLS init connection failed - %s\n", gnutls_strerror(ret
)));
561 talloc_free(new_sock
);
567 setup for a new client connection
569 struct socket_context
*tls_init_client(struct socket_context
*socket_ctx
,
570 struct tevent_fd
*fde
,
573 struct tls_context
*tls
;
575 const int cert_type_priority
[] = { GNUTLS_CRT_X509
, GNUTLS_CRT_OPENPGP
, 0 };
576 struct socket_context
*new_sock
;
579 nt_status
= socket_create_with_ops(socket_ctx
, &tls_socket_ops
, &new_sock
,
581 socket_ctx
->flags
| SOCKET_FLAG_ENCRYPT
);
582 if (!NT_STATUS_IS_OK(nt_status
)) {
586 tls
= talloc(new_sock
, struct tls_context
);
587 if (tls
== NULL
) return NULL
;
589 tls
->socket
= socket_ctx
;
590 talloc_steal(tls
, socket_ctx
);
593 new_sock
->private_data
= tls
;
595 gnutls_global_init();
597 gnutls_certificate_allocate_credentials(&tls
->xcred
);
598 gnutls_certificate_set_x509_trust_file(tls
->xcred
, ca_path
, GNUTLS_X509_FMT_PEM
);
599 TLSCHECK(gnutls_init(&tls
->session
, GNUTLS_CLIENT
));
600 TLSCHECK(gnutls_set_default_priority(tls
->session
));
601 gnutls_certificate_type_set_priority(tls
->session
, cert_type_priority
);
602 TLSCHECK(gnutls_credentials_set(tls
->session
, GNUTLS_CRD_CERTIFICATE
, tls
->xcred
));
604 talloc_set_destructor(tls
, tls_destructor
);
606 gnutls_transport_set_ptr(tls
->session
, (gnutls_transport_ptr
)tls
);
607 gnutls_transport_set_pull_function(tls
->session
, (gnutls_pull_func
)tls_pull
);
608 gnutls_transport_set_push_function(tls
->session
, (gnutls_push_func
)tls_push
);
609 #if GNUTLS_VERSION_MAJOR < 3
610 gnutls_transport_set_lowat(tls
->session
, 0);
612 tls
->tls_detect
= false;
614 tls
->output_pending
= false;
615 tls
->done_handshake
= false;
616 tls
->have_first_byte
= false;
617 tls
->tls_enabled
= true;
618 tls
->interrupted
= false;
620 new_sock
->state
= SOCKET_STATE_CLIENT_CONNECTED
;
625 DEBUG(0,("TLS init connection failed - %s\n", gnutls_strerror(ret
)));
626 tls
->tls_enabled
= false;
630 static NTSTATUS
tls_socket_set_option(struct socket_context
*sock
, const char *option
, const char *val
)
632 set_socket_options(socket_get_fd(sock
), option
);
636 static char *tls_socket_get_peer_name(struct socket_context
*sock
, TALLOC_CTX
*mem_ctx
)
638 struct tls_context
*tls
= talloc_get_type(sock
->private_data
, struct tls_context
);
639 return socket_get_peer_name(tls
->socket
, mem_ctx
);
642 static struct socket_address
*tls_socket_get_peer_addr(struct socket_context
*sock
, TALLOC_CTX
*mem_ctx
)
644 struct tls_context
*tls
= talloc_get_type(sock
->private_data
, struct tls_context
);
645 return socket_get_peer_addr(tls
->socket
, mem_ctx
);
648 static struct socket_address
*tls_socket_get_my_addr(struct socket_context
*sock
, TALLOC_CTX
*mem_ctx
)
650 struct tls_context
*tls
= talloc_get_type(sock
->private_data
, struct tls_context
);
651 return socket_get_my_addr(tls
->socket
, mem_ctx
);
654 static int tls_socket_get_fd(struct socket_context
*sock
)
656 struct tls_context
*tls
= talloc_get_type(sock
->private_data
, struct tls_context
);
657 return socket_get_fd(tls
->socket
);
660 static const struct socket_ops tls_socket_ops
= {
662 .fn_init
= tls_socket_init
,
663 .fn_recv
= tls_socket_recv
,
664 .fn_send
= tls_socket_send
,
665 .fn_pending
= tls_socket_pending
,
667 .fn_set_option
= tls_socket_set_option
,
669 .fn_get_peer_name
= tls_socket_get_peer_name
,
670 .fn_get_peer_addr
= tls_socket_get_peer_addr
,
671 .fn_get_my_addr
= tls_socket_get_my_addr
,
672 .fn_get_fd
= tls_socket_get_fd
677 /* for systems without tls we just fail the operations, and the caller
678 * will retain the original socket */
680 struct tls_params
*tls_initialise(TALLOC_CTX
*mem_ctx
, struct loadparm_context
*lp_ctx
)
682 return talloc_new(mem_ctx
);
686 setup for a new connection
688 struct socket_context
*tls_init_server(struct tls_params
*params
,
689 struct socket_context
*socket
,
690 struct tevent_fd
*fde
,
691 const char *plain_chars
)
698 setup for a new client connection
700 struct socket_context
*tls_init_client(struct socket_context
*socket
,
701 struct tevent_fd
*fde
,