2 * Copyright (C) 2004-2012 Free Software Foundation, Inc.
3 * Copyright (C) 2001,2002 Paul Sheer
4 * Portions Copyright (C) 2002,2003 Nikos Mavrogiannopoulos
6 * This file is part of GnuTLS.
8 * GnuTLS 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 3 of the License, or
11 * (at your option) any later version.
13 * GnuTLS 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.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 /* This server is heavily modified for GnuTLS by Nikos Mavrogiannopoulos
23 * (which means it is quite unreadable)
29 #include "serv-args.h"
33 #include <sys/types.h>
35 #include <gnutls/gnutls.h>
36 #include <gnutls/dtls.h>
37 #include <gnutls/openpgp.h>
39 #include <sys/select.h>
46 /* Gnulib portability files. */
48 #include "version-etc.h"
49 #include "read-file.h"
54 /* konqueror cannot handle sending the page in multiple
58 static int generate
= 0;
63 unsigned int verbose
= 1;
67 int disable_client_cert
;
69 const char *psk_passwd
= NULL
;
70 const char *srp_passwd
= NULL
;
71 const char *srp_passwd_conf
= NULL
;
72 const char *pgp_keyring
= NULL
;
73 const char *pgp_keyfile
= NULL
;
74 const char *pgp_certfile
= NULL
;
75 const char *x509_keyfile
= NULL
;
76 const char *x509_certfile
= NULL
;
77 const char *x509_dsakeyfile
= NULL
;
78 const char *x509_dsacertfile
= NULL
;
79 const char *x509_ecckeyfile
= NULL
;
80 const char *x509_ecccertfile
= NULL
;
81 const char *x509_cafile
= NULL
;
82 const char *dh_params_file
= NULL
;
83 const char *x509_crlfile
= NULL
;
84 const char * priorities
= NULL
;
85 const char * status_response_ocsp
= NULL
;
87 gnutls_datum_t session_ticket_key
;
88 static void tcp_server (const char *name
, int port
);
92 /* This is a sample TCP echo server.
93 * This will behave as an http server if any argument in the
94 * command line is present
97 #define SMALL_READ_TEST (2147483647)
99 #define GERR(ret) fprintf(stdout, "Error: %s\n", safe_strerror(ret))
101 #define HTTP_END "</BODY></HTML>\n\n"
103 #define HTTP_UNIMPLEMENTED "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r\n<HTML><HEAD>\r\n<TITLE>501 Method Not Implemented</TITLE>\r\n</HEAD><BODY>\r\n<H1>Method Not Implemented</H1>\r\n<HR>\r\n</BODY></HTML>\r\n"
104 #define HTTP_OK "HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n"
106 #define HTTP_BEGIN HTTP_OK \
109 "<CENTER><H1>This is <a href=\"http://www.gnu.org/software/gnutls\">" \
110 "GnuTLS</a></H1></CENTER>\n\n"
112 /* These are global */
113 gnutls_srp_server_credentials_t srp_cred
= NULL
;
114 gnutls_psk_server_credentials_t psk_cred
= NULL
;
115 gnutls_anon_server_credentials_t dh_cred
= NULL
;
116 gnutls_certificate_credentials_t cert_cred
= NULL
;
118 const int ssl_session_cache
= 128;
120 static void wrap_db_init (void);
121 static void wrap_db_deinit (void);
122 static int wrap_db_store (void *dbf
, gnutls_datum_t key
, gnutls_datum_t data
);
123 static gnutls_datum_t
wrap_db_fetch (void *dbf
, gnutls_datum_t key
);
124 static int wrap_db_delete (void *dbf
, gnutls_datum_t key
);
126 static void cmd_parser (int argc
, char **argv
);
129 #define HTTP_STATE_REQUEST 1
130 #define HTTP_STATE_RESPONSE 2
131 #define HTTP_STATE_CLOSING 3
133 LIST_TYPE_DECLARE (listener_item
, char *http_request
;
134 char *http_response
; int request_length
;
135 int response_length
; int response_written
;
136 int http_state
; int listen_socket
;
137 int fd
; gnutls_session_t tls_session
; int handshake_ok
;);
140 safe_strerror (int value
)
142 const char *ret
= gnutls_strerror (value
);
149 listener_free (listener_item
* j
)
152 free (j
->http_request
);
153 free (j
->http_response
);
156 gnutls_bye (j
->tls_session
, GNUTLS_SHUT_WR
);
159 gnutls_deinit (j
->tls_session
);
164 /* we use primes up to 1024 in this server.
165 * otherwise we should add them here.
168 gnutls_dh_params_t dh_params
= NULL
;
169 gnutls_rsa_params_t rsa_params
= NULL
;
172 generate_dh_primes (void)
175 gnutls_sec_param_to_pk_bits (GNUTLS_PK_DH
, GNUTLS_SEC_PARAM_NORMAL
);
177 if (gnutls_dh_params_init (&dh_params
) < 0)
179 fprintf (stderr
, "Error in dh parameter initialization\n");
183 /* Generate Diffie-Hellman parameters - for use with DHE
184 * kx algorithms. These should be discarded and regenerated
185 * once a week or once a month. Depends on the
186 * security requirements.
189 ("Generating Diffie-Hellman parameters [%d]. Please wait...\n",
193 if (gnutls_dh_params_generate2 (dh_params
, prime_bits
) < 0)
195 fprintf (stderr
, "Error in prime generation\n");
203 read_dh_params (void)
207 gnutls_datum_t params
;
210 if (gnutls_dh_params_init (&dh_params
) < 0)
212 fprintf (stderr
, "Error in dh parameter initialization\n");
216 /* read the params file
218 fd
= fopen (dh_params_file
, "r");
221 fprintf (stderr
, "Could not open %s\n", dh_params_file
);
225 size
= fread (tmpdata
, 1, sizeof (tmpdata
) - 1, fd
);
229 params
.data
= (unsigned char *) tmpdata
;
233 gnutls_dh_params_import_pkcs3 (dh_params
, ¶ms
, GNUTLS_X509_FMT_PEM
);
237 fprintf (stderr
, "Error parsing dh params: %s\n", safe_strerror (size
));
241 printf ("Read Diffie-Hellman parameters.\n");
246 static char pkcs3
[] =
247 "-----BEGIN DH PARAMETERS-----\n"
248 "MIGGAoGAtkxw2jlsVCsrfLqxrN+IrF/3W8vVFvDzYbLmxi2GQv9s/PQGWP1d9i22\n"
249 "P2DprfcJknWt7KhCI1SaYseOQIIIAYP78CfyIpGScW/vS8khrw0rlQiyeCvQgF3O\n"
250 "GeGOEywcw+oQT4SmFOD7H0smJe2CNyjYpexBXQ/A0mbTF9QKm1cCAQU=\n"
251 "-----END DH PARAMETERS-----\n";
254 static_dh_params (void)
256 gnutls_datum_t params
= { (void *) pkcs3
, sizeof (pkcs3
) };
259 if (gnutls_dh_params_init (&dh_params
) < 0)
261 fprintf (stderr
, "Error in dh parameter initialization\n");
265 ret
= gnutls_dh_params_import_pkcs3 (dh_params
, ¶ms
,
266 GNUTLS_X509_FMT_PEM
);
270 fprintf (stderr
, "Error parsing dh params: %s\n", safe_strerror (ret
));
274 printf ("Set static Diffie-Hellman parameters, consider --dhparams.\n");
280 get_params (gnutls_session_t session
, gnutls_params_type_t type
,
281 gnutls_params_st
* st
)
284 if (type
== GNUTLS_PARAMS_RSA_EXPORT
)
286 if (rsa_params
== NULL
)
288 st
->params
.rsa_export
= rsa_params
;
290 else if (type
== GNUTLS_PARAMS_DH
)
292 if (dh_params
== NULL
)
294 st
->params
.dh
= dh_params
;
306 generate_rsa_params (void)
308 if (gnutls_rsa_params_init (&rsa_params
) < 0)
310 fprintf (stderr
, "Error in rsa parameter initialization\n");
314 /* Generate RSA parameters - for use with RSA-export
315 * cipher suites. These should be discarded and regenerated
316 * once a day, once every 500 transactions etc. Depends on the
317 * security requirements.
319 printf ("Generating temporary RSA parameters. Please wait...\n");
322 if (gnutls_rsa_params_generate2 (rsa_params
, 512) < 0)
324 fprintf (stderr
, "Error in rsa parameter generation\n");
331 LIST_DECLARE_INIT (listener_list
, listener_item
, listener_free
);
333 gnutls_session_t
initialize_session (int dtls
)
335 gnutls_session_t session
;
339 if (priorities
== NULL
)
340 priorities
= "NORMAL";
343 gnutls_init (&session
, GNUTLS_SERVER
| GNUTLS_DATAGRAM
);
345 gnutls_init (&session
, GNUTLS_SERVER
);
347 /* allow the use of private ciphersuites.
349 gnutls_handshake_set_private_extensions (session
, 1);
353 gnutls_db_set_retrieve_function (session
, wrap_db_fetch
);
354 gnutls_db_set_remove_function (session
, wrap_db_delete
);
355 gnutls_db_set_store_function (session
, wrap_db_store
);
356 gnutls_db_set_ptr (session
, NULL
);
358 #ifdef ENABLE_SESSION_TICKET
360 gnutls_session_ticket_enable_server (session
, &session_ticket_key
);
365 gnutls_session_ticket_enable_server (session
, &session_ticket_key
);
367 if (gnutls_priority_set_direct (session
, priorities
, &err
) < 0)
369 fprintf (stderr
, "Syntax error at: %s\n", err
);
373 gnutls_credentials_set (session
, GNUTLS_CRD_ANON
, dh_cred
);
375 if (srp_cred
!= NULL
)
376 gnutls_credentials_set (session
, GNUTLS_CRD_SRP
, srp_cred
);
378 if (psk_cred
!= NULL
)
379 gnutls_credentials_set (session
, GNUTLS_CRD_PSK
, psk_cred
);
381 if (cert_cred
!= NULL
)
382 gnutls_credentials_set (session
, GNUTLS_CRD_CERTIFICATE
, cert_cred
);
384 if (disable_client_cert
)
385 gnutls_certificate_server_set_request (session
, GNUTLS_CERT_IGNORE
);
389 gnutls_certificate_server_set_request (session
, GNUTLS_CERT_REQUIRE
);
391 gnutls_certificate_server_set_request (session
, GNUTLS_CERT_REQUEST
);
394 if (HAVE_OPT (HEARTBEAT
))
395 gnutls_heartbeat_enable(session
, GNUTLS_HB_PEER_ALLOWED_TO_SEND
);
397 if (HAVE_OPT (SRTP_PROFILES
))
399 ret
= gnutls_srtp_set_profile_direct (session
, OPT_ARG(SRTP_PROFILES
), &err
);
400 if (ret
== GNUTLS_E_INVALID_REQUEST
) fprintf (stderr
, "Syntax error at: %s\n", err
);
402 fprintf(stderr
, "Error in profiles: %s\n", gnutls_strerror(ret
));
409 #include <gnutls/x509.h>
411 static const char DEFAULT_DATA
[] =
412 "This is the default message reported by the GnuTLS implementation. "
413 "For more information please visit "
414 "<a href=\"http://www.gnutls.org/\">http://www.gnutls.org/</a>.";
416 /* Creates html with the current session information.
418 #define tmp_buffer &http_buffer[strlen(http_buffer)]
419 #define tmp_buffer_size len-strlen(http_buffer)
421 peer_print_info (gnutls_session_t session
, int *ret_length
,
425 unsigned char sesid
[32];
426 size_t i
, sesid_size
;
428 gnutls_kx_algorithm_t kx_alg
;
429 size_t len
= 20 * 1024 + strlen (header
);
430 char *crtinfo
= NULL
;
435 http_buffer
= malloc (len
);
436 if (http_buffer
== NULL
)
439 strcpy (http_buffer
, HTTP_BEGIN
);
440 strcpy (&http_buffer
[sizeof (HTTP_BEGIN
) - 1], DEFAULT_DATA
);
441 strcpy (&http_buffer
[sizeof (HTTP_BEGIN
) + sizeof (DEFAULT_DATA
) - 2],
444 sizeof (DEFAULT_DATA
) + sizeof (HTTP_BEGIN
) + sizeof (HTTP_END
) - 3;
448 if (gnutls_certificate_type_get (session
) == GNUTLS_CRT_X509
)
450 const gnutls_datum_t
*cert_list
;
451 unsigned int cert_list_size
= 0;
453 cert_list
= gnutls_certificate_get_peers (session
, &cert_list_size
);
455 for (i
= 0; i
< cert_list_size
; i
++)
457 gnutls_x509_crt_t cert
;
460 if (gnutls_x509_crt_init (&cert
) == 0 &&
461 gnutls_x509_crt_import (cert
, &cert_list
[i
],
462 GNUTLS_X509_FMT_DER
) == 0 &&
463 gnutls_x509_crt_print (cert
, GNUTLS_CRT_PRINT_FULL
, &info
) == 0)
465 const char *post
= "</PRE><P><PRE>";
467 crtinfo
= realloc (crtinfo
, ncrtinfo
+ info
.size
+
471 memcpy (crtinfo
+ ncrtinfo
, info
.data
, info
.size
);
472 ncrtinfo
+= info
.size
;
473 memcpy (crtinfo
+ ncrtinfo
, post
, strlen (post
));
474 ncrtinfo
+= strlen (post
);
475 crtinfo
[ncrtinfo
] = '\0';
476 gnutls_free (info
.data
);
481 http_buffer
= malloc (len
);
482 if (http_buffer
== NULL
)
488 strcpy (http_buffer
, HTTP_BEGIN
);
490 /* print session_id */
491 sesid_size
= sizeof(sesid
);
492 gnutls_session_get_id (session
, sesid
, &sesid_size
);
493 snprintf (tmp_buffer
, tmp_buffer_size
, "\n<p>Session ID: <i>");
494 for (i
= 0; i
< sesid_size
; i
++)
495 snprintf (tmp_buffer
, tmp_buffer_size
, "%.2X", sesid
[i
]);
496 snprintf (tmp_buffer
, tmp_buffer_size
, "</i></p>\n");
497 snprintf (tmp_buffer
, tmp_buffer_size
,
498 "<h5>If your browser supports session resuming, then you should see the "
499 "same session ID, when you press the <b>reload</b> button.</h5>\n");
501 /* Here unlike print_info() we use the kx algorithm to distinguish
502 * the functions to call.
506 size_t dns_size
= sizeof (dns
);
509 if (gnutls_server_name_get (session
, dns
, &dns_size
, &type
, 0) == 0)
511 snprintf (tmp_buffer
, tmp_buffer_size
, "\n<p>Server Name: %s</p>\n",
517 kx_alg
= gnutls_kx_get (session
);
519 /* print srp specific data */
521 if (kx_alg
== GNUTLS_KX_SRP
)
523 snprintf (tmp_buffer
, tmp_buffer_size
,
524 "<p>Connected as user '%s'.</p>\n",
525 gnutls_srp_server_get_username (session
));
530 if (kx_alg
== GNUTLS_KX_PSK
)
532 snprintf (tmp_buffer
, tmp_buffer_size
,
533 "<p>Connected as user '%s'.</p>\n",
534 gnutls_psk_server_get_username (session
));
539 if (kx_alg
== GNUTLS_KX_ANON_DH
)
541 snprintf (tmp_buffer
, tmp_buffer_size
,
542 "<p> Connect using anonymous DH (prime of %d bits)</p>\n",
543 gnutls_dh_get_prime_bits (session
));
547 if (kx_alg
== GNUTLS_KX_DHE_RSA
|| kx_alg
== GNUTLS_KX_DHE_DSS
)
549 snprintf (tmp_buffer
, tmp_buffer_size
,
550 "Ephemeral DH using prime of <b>%d</b> bits.<br>\n",
551 gnutls_dh_get_prime_bits (session
));
554 /* print session information */
555 strcat (http_buffer
, "<P>\n");
557 tmp
= gnutls_protocol_get_name (gnutls_protocol_get_version (session
));
560 snprintf (tmp_buffer
, tmp_buffer_size
,
561 "<TABLE border=1><TR><TD>Protocol version:</TD><TD>%s</TD></TR>\n",
564 if (gnutls_auth_get_type (session
) == GNUTLS_CRD_CERTIFICATE
)
567 gnutls_certificate_type_get_name (gnutls_certificate_type_get
571 snprintf (tmp_buffer
, tmp_buffer_size
,
572 "<TR><TD>Certificate Type:</TD><TD>%s</TD></TR>\n", tmp
);
575 tmp
= gnutls_kx_get_name (kx_alg
);
578 snprintf (tmp_buffer
, tmp_buffer_size
,
579 "<TR><TD>Key Exchange:</TD><TD>%s</TD></TR>\n", tmp
);
581 tmp
= gnutls_compression_get_name (gnutls_compression_get (session
));
584 snprintf (tmp_buffer
, tmp_buffer_size
,
585 "<TR><TD>Compression</TD><TD>%s</TD></TR>\n", tmp
);
587 tmp
= gnutls_cipher_get_name (gnutls_cipher_get (session
));
590 snprintf (tmp_buffer
, tmp_buffer_size
,
591 "<TR><TD>Cipher</TD><TD>%s</TD></TR>\n", tmp
);
593 tmp
= gnutls_mac_get_name (gnutls_mac_get (session
));
596 snprintf (tmp_buffer
, tmp_buffer_size
, "<TR><TD>MAC</TD><TD>%s</TD></TR>\n",
599 tmp
= gnutls_cipher_suite_get_name (kx_alg
,
600 gnutls_cipher_get (session
),
601 gnutls_mac_get (session
));
604 snprintf (tmp_buffer
, tmp_buffer_size
,
605 "<TR><TD>Ciphersuite</TD><TD>%s</TD></TR></p></TABLE>\n", tmp
);
609 snprintf (tmp_buffer
, tmp_buffer_size
, "<hr><PRE>%s\n</PRE>\n",
614 snprintf (tmp_buffer
, tmp_buffer_size
,
615 "<hr><P>Your HTTP header was:<PRE>%s</PRE></P>\n" HTTP_END
,
618 *ret_length
= strlen (http_buffer
);
624 human_addr (const struct sockaddr
*sa
, socklen_t salen
,
625 char *buf
, size_t buflen
)
627 const char *save_buf
= buf
;
635 switch (sa
->sa_family
)
639 snprintf (buf
, buflen
, "IPv6 ");
644 snprintf (buf
, buflen
, "IPv4 ");
652 if (getnameinfo (sa
, salen
, buf
, buflen
, NULL
, 0, NI_NUMERICHOST
) != 0)
659 strncat (buf
, " port ", buflen
);
665 if (getnameinfo (sa
, salen
, NULL
, 0, buf
, buflen
, NI_NUMERICSERV
) != 0)
672 wait_for_connection (void)
682 lloopstart (listener_list
, j
)
684 if (j
->listen_socket
)
690 lloopend (listener_list
, j
);
693 n
= select (n
+ 1, &rd
, &wr
, NULL
, NULL
);
694 if (n
== -1 && errno
== EINTR
)
702 /* find which one is ready */
703 lloopstart (listener_list
, j
)
705 /* a new connection has arrived */
706 if (FD_ISSET (j
->fd
, &rd
) && j
->listen_socket
)
712 lloopend (listener_list
, j
);
717 listen_socket (const char *name
, int listen_port
, int socktype
)
719 struct addrinfo hints
, *res
, *ptr
;
723 listener_item
*j
= NULL
;
725 snprintf (portname
, sizeof (portname
), "%d", listen_port
);
726 memset (&hints
, 0, sizeof (hints
));
727 hints
.ai_socktype
= socktype
;
728 hints
.ai_flags
= AI_PASSIVE
734 if ((s
= getaddrinfo (NULL
, portname
, &hints
, &res
)) != 0)
736 fprintf (stderr
, "getaddrinfo() failed: %s\n", gai_strerror (s
));
740 for (ptr
= res
; ptr
!= NULL
; ptr
= ptr
->ai_next
)
743 if (ptr
->ai_family
!= AF_INET
)
747 /* Print what we are doing. */
751 fprintf (stderr
, "%s listening on %s...",
752 name
, human_addr (ptr
->ai_addr
, ptr
->ai_addrlen
,
753 topbuf
, sizeof (topbuf
)));
756 if ((s
= socket (ptr
->ai_family
, ptr
->ai_socktype
,
757 ptr
->ai_protocol
)) < 0)
759 perror ("socket() failed");
763 #if defined(HAVE_IPV6) && !defined(_WIN32)
764 if (ptr
->ai_family
== AF_INET6
)
767 /* avoid listen on ipv6 addresses failing
768 * because already listening on ipv4 addresses: */
769 setsockopt (s
, IPPROTO_IPV6
, IPV6_V6ONLY
,
770 (const void *) &yes
, sizeof (yes
));
774 if (socktype
== SOCK_STREAM
)
777 if (setsockopt (s
, SOL_SOCKET
, SO_REUSEADDR
,
778 (const void *) &yes
, sizeof (yes
)) < 0)
780 perror ("setsockopt() failed");
787 #if defined(IP_DONTFRAG)
789 if (setsockopt (s
, IPPROTO_IP
, IP_DONTFRAG
,
790 (const void *) &yes
, sizeof (yes
)) < 0)
791 perror ("setsockopt(IP_DF) failed");
792 #elif defined(IP_MTU_DISCOVER)
793 yes
= IP_PMTUDISC_DO
;
794 if (setsockopt (s
, IPPROTO_IP
, IP_MTU_DISCOVER
,
795 (const void *) &yes
, sizeof (yes
)) < 0)
796 perror ("setsockopt(IP_DF) failed");
800 if (bind (s
, ptr
->ai_addr
, ptr
->ai_addrlen
) < 0)
802 perror ("bind() failed");
807 if (socktype
== SOCK_STREAM
)
809 if (listen (s
, 10) < 0)
811 perror ("listen() failed");
816 /* new list entry for the connection */
817 lappend (listener_list
);
818 j
= listener_list
.tail
;
819 j
->listen_socket
= 1;
822 /* Complete earlier message. */
823 fprintf (stderr
, "done\n");
833 /* strips \r\n from the end of the string
839 int len
= strlen (data
);
841 for (i
= 0; i
< len
; i
++)
843 if (data
[i
] == '\r' && data
[i
+ 1] == '\n' && data
[i
+ 1] == 0)
853 get_response (gnutls_session_t session
, char *request
,
854 char **response
, int *response_length
)
860 if (strncmp (request
, "GET ", 4))
863 if (!(h
= strchr (request
, '\n')))
867 while (*h
== '\r' || *h
== '\n')
870 if (!(p
= strchr (request
+ 4, ' ')))
874 /* *response = peer_print_info(session, request+4, h, response_length); */
877 *response
= peer_print_info (session
, response_length
, h
);
882 fprintf (stderr
, "received: %s\n", request
);
883 if (check_command (session
, request
))
886 *response_length
= 0;
889 *response
= strdup (request
);
890 *response_length
= ((*response
) ? strlen (*response
) : 0);
896 *response
= strdup (HTTP_UNIMPLEMENTED
);
897 *response_length
= ((*response
) ? strlen (*response
) : 0);
900 static void terminate (int sig
) __attribute__ ((noreturn
));
905 fprintf (stderr
, "Exiting via signal %d\n", sig
);
911 check_alert (gnutls_session_t session
, int ret
)
913 if (ret
== GNUTLS_E_WARNING_ALERT_RECEIVED
914 || ret
== GNUTLS_E_FATAL_ALERT_RECEIVED
)
916 int last_alert
= gnutls_alert_get (session
);
917 if (last_alert
== GNUTLS_A_NO_RENEGOTIATION
&&
918 ret
== GNUTLS_E_WARNING_ALERT_RECEIVED
)
920 ("* Received NO_RENEGOTIATION alert. Client does not support renegotiation.\n");
922 printf ("* Received alert '%d': %s.\n", last_alert
,
923 gnutls_alert_get_name (last_alert
));
928 tls_log_func (int level
, const char *str
)
930 fprintf (stderr
, "|<%d>| %s", level
, str
);
934 tls_audit_log_func (gnutls_session_t session
, const char *str
)
936 fprintf (stderr
, "|<%p>| %s", session
, str
);
940 main (int argc
, char **argv
)
945 set_program_name (argv
[0]);
946 cmd_parser (argc
, argv
);
949 signal (SIGHUP
, SIG_IGN
);
950 signal (SIGTERM
, terminate
);
951 if (signal (SIGINT
, terminate
) == SIG_IGN
)
952 signal (SIGINT
, SIG_IGN
); /* e.g. background process */
961 strcpy (name
, "UDP ");
967 strcat (name
, "HTTP Server");
971 strcat (name
, "Echo Server");
974 gnutls_global_set_log_function (tls_log_func
);
975 gnutls_global_set_audit_log_function (tls_audit_log_func
);
976 gnutls_global_set_log_level (debug
);
978 if ((ret
= gnutls_global_init ()) < 0)
980 fprintf (stderr
, "global_init: %s\n", gnutls_strerror (ret
));
988 /* Note that servers must generate parameters for
989 * Diffie-Hellman. See gnutls_dh_params_generate(), and
990 * gnutls_dh_params_set().
994 generate_rsa_params ();
995 generate_dh_primes ();
997 else if (dh_params_file
)
1003 static_dh_params ();
1006 if (gnutls_certificate_allocate_credentials (&cert_cred
) < 0)
1008 fprintf (stderr
, "memory error\n");
1012 if (x509_cafile
!= NULL
)
1014 if ((ret
= gnutls_certificate_set_x509_trust_file
1015 (cert_cred
, x509_cafile
, x509ctype
)) < 0)
1017 fprintf (stderr
, "Error reading '%s'\n", x509_cafile
);
1023 printf ("Processed %d CA certificate(s).\n", ret
);
1026 if (x509_crlfile
!= NULL
)
1028 if ((ret
= gnutls_certificate_set_x509_crl_file
1029 (cert_cred
, x509_crlfile
, x509ctype
)) < 0)
1031 fprintf (stderr
, "Error reading '%s'\n", x509_crlfile
);
1037 printf ("Processed %d CRL(s).\n", ret
);
1041 #ifdef ENABLE_OPENPGP
1042 if (pgp_keyring
!= NULL
)
1045 gnutls_certificate_set_openpgp_keyring_file (cert_cred
, pgp_keyring
,
1046 GNUTLS_OPENPGP_FMT_BASE64
);
1049 fprintf (stderr
, "Error setting the OpenPGP keyring file\n");
1054 if (HAVE_OPT (PGPCERTFILE
))
1056 if (HAVE_OPT (PGPSUBKEY
))
1057 ret
= gnutls_certificate_set_openpgp_key_file2
1058 (cert_cred
, pgp_certfile
, pgp_keyfile
, OPT_ARG (PGPSUBKEY
),
1059 GNUTLS_OPENPGP_FMT_BASE64
);
1061 ret
= gnutls_certificate_set_openpgp_key_file
1062 (cert_cred
, pgp_certfile
, pgp_keyfile
, GNUTLS_OPENPGP_FMT_BASE64
);
1067 "Error[%d] while reading the OpenPGP key pair ('%s', '%s')\n",
1068 ret
, pgp_certfile
, pgp_keyfile
);
1074 if (x509_certfile
!= NULL
)
1075 if ((ret
= gnutls_certificate_set_x509_key_file
1076 (cert_cred
, x509_certfile
, x509_keyfile
, x509ctype
)) < 0)
1079 "Error reading '%s' or '%s'\n", x509_certfile
, x509_keyfile
);
1084 if (x509_dsacertfile
!= NULL
)
1085 if ((ret
= gnutls_certificate_set_x509_key_file
1086 (cert_cred
, x509_dsacertfile
, x509_dsakeyfile
, x509ctype
)) < 0)
1088 fprintf (stderr
, "Error reading '%s' or '%s'\n",
1089 x509_dsacertfile
, x509_dsakeyfile
);
1094 if (x509_ecccertfile
!= NULL
)
1095 if ((ret
= gnutls_certificate_set_x509_key_file
1096 (cert_cred
, x509_ecccertfile
, x509_ecckeyfile
, x509ctype
)) < 0)
1098 fprintf (stderr
, "Error reading '%s' or '%s'\n",
1099 x509_ecccertfile
, x509_ecckeyfile
);
1104 /* OCSP status-request TLS extension */
1105 if (status_response_ocsp
)
1107 if (gnutls_certificate_set_ocsp_status_request_file (cert_cred
, status_response_ocsp
, 0) < 0)
1109 fprintf (stderr
, "Cannot set OCSP status request file: %s\n", gnutls_strerror(ret
));
1114 gnutls_certificate_set_params_function (cert_cred
, get_params
);
1115 /* gnutls_certificate_set_dh_params(cert_cred, dh_params);
1116 * gnutls_certificate_set_rsa_export_params(cert_cred, rsa_params);
1119 /* this is a password file (created with the included srpcrypt utility)
1120 * Read README.crypt prior to using SRP.
1123 if (srp_passwd
!= NULL
)
1125 gnutls_srp_allocate_server_credentials (&srp_cred
);
1128 gnutls_srp_set_server_credentials_file (srp_cred
, srp_passwd
,
1129 srp_passwd_conf
)) < 0)
1131 /* only exit is this function is not disabled
1133 fprintf (stderr
, "Error while setting SRP parameters\n");
1139 /* this is a password file
1142 if (psk_passwd
!= NULL
)
1144 gnutls_psk_allocate_server_credentials (&psk_cred
);
1147 gnutls_psk_set_server_credentials_file (psk_cred
, psk_passwd
)) < 0)
1149 /* only exit is this function is not disabled
1151 fprintf (stderr
, "Error while setting PSK parameters\n");
1155 if (HAVE_OPT (PSKHINT
))
1157 ret
= gnutls_psk_set_server_credentials_hint (psk_cred
,
1161 fprintf (stderr
, "Error setting PSK identity hint.\n");
1166 gnutls_psk_set_server_params_function (psk_cred
, get_params
);
1171 gnutls_anon_allocate_server_credentials (&dh_cred
);
1172 gnutls_anon_set_server_params_function (dh_cred
, get_params
);
1174 /* gnutls_anon_set_server_dh_params(dh_cred, dh_params); */
1177 #ifdef ENABLE_SESSION_TICKET
1179 gnutls_session_ticket_key_generate (&session_ticket_key
);
1183 mtu
= OPT_VALUE_MTU
;
1187 if (HAVE_OPT (PORT
))
1188 port
= OPT_VALUE_PORT
;
1193 udp_server (name
, port
, mtu
);
1195 tcp_server (name
, port
);
1199 tcp_server (const char *name
, int port
)
1204 struct sockaddr_storage client_address
;
1207 s
= listen_socket (name
, port
, SOCK_STREAM
);
1223 /* flag which connections we are reading or writing to within the fd sets */
1224 lloopstart (listener_list
, j
)
1228 val
= fcntl (j
->fd
, F_GETFL
, 0);
1229 if ((val
== -1) || (fcntl (j
->fd
, F_SETFL
, val
| O_NONBLOCK
) < 0))
1236 if (j
->listen_socket
)
1238 FD_SET (j
->fd
, &rd
);
1241 if (j
->http_state
== HTTP_STATE_REQUEST
)
1243 FD_SET (j
->fd
, &rd
);
1246 if (j
->http_state
== HTTP_STATE_RESPONSE
)
1248 FD_SET (j
->fd
, &wr
);
1252 lloopend (listener_list
, j
);
1254 /* core operation */
1255 n
= select (n
+ 1, &rd
, &wr
, NULL
, NULL
);
1256 if (n
== -1 && errno
== EINTR
)
1260 perror ("select()");
1264 /* read or write to each connection as indicated by select()'s return argument */
1265 lloopstart (listener_list
, j
)
1268 /* a new connection has arrived */
1269 if (FD_ISSET (j
->fd
, &rd
) && j
->listen_socket
)
1271 gnutls_session_t tls_session
;
1273 tls_session
= initialize_session (0);
1275 calen
= sizeof (client_address
);
1276 memset (&client_address
, 0, calen
);
1277 accept_fd
= accept (j
->fd
, (struct sockaddr
*) &client_address
,
1282 perror ("accept()");
1289 /* new list entry for the connection */
1290 lappend (listener_list
);
1291 j
= listener_list
.tail
;
1292 j
->http_request
= (char *) strdup ("");
1293 j
->http_state
= HTTP_STATE_REQUEST
;
1296 j
->tls_session
= tls_session
;
1297 gnutls_transport_set_ptr (tls_session
,
1298 (gnutls_transport_ptr_t
)
1299 gl_fd_to_handle (accept_fd
));
1300 j
->handshake_ok
= 0;
1306 ctt
[strlen (ctt
) - 1] = 0;
1308 printf ("\n* Accepted connection from %s on %s\n",
1309 human_addr ((struct sockaddr
*)
1310 &client_address
, calen
, topbuf
,
1311 sizeof (topbuf
)), ctt
);
1316 if (FD_ISSET (j
->fd
, &rd
) && !j
->listen_socket
)
1318 /* read partial GET request */
1322 if (j
->handshake_ok
== 0)
1324 r
= gnutls_handshake (j
->tls_session
);
1325 if (r
< 0 && gnutls_error_is_fatal (r
) == 0)
1327 check_alert (j
->tls_session
, r
);
1330 else if (r
< 0 && gnutls_error_is_fatal (r
) == 1)
1332 check_alert (j
->tls_session
, r
);
1333 fprintf (stderr
, "Error in handshake\n");
1339 gnutls_alert_send_appropriate (j
->tls_session
, r
);
1341 while (ret
== GNUTLS_E_AGAIN
1342 || ret
== GNUTLS_E_INTERRUPTED
);
1343 j
->http_state
= HTTP_STATE_CLOSING
;
1347 if (gnutls_session_is_resumed (j
->tls_session
) != 0
1349 printf ("*** This is a resumed session\n");
1353 printf ("\n* Successful handshake from %s\n",
1354 human_addr ((struct sockaddr
*)
1355 &client_address
, calen
, topbuf
,
1357 print_info (j
->tls_session
, verbose
, verbose
);
1358 if (gnutls_auth_get_type (j
->tls_session
) ==
1359 GNUTLS_CRD_CERTIFICATE
)
1360 cert_verify (j
->tls_session
, NULL
);
1362 j
->handshake_ok
= 1;
1366 if (j
->handshake_ok
== 1)
1368 r
= gnutls_record_recv (j
->tls_session
, buf
,
1369 MIN (1024, SMALL_READ_TEST
));
1370 if (r
== GNUTLS_E_HEARTBEAT_PING_RECEIVED
)
1372 gnutls_heartbeat_pong(j
->tls_session
, 0);
1374 if (r
== GNUTLS_E_INTERRUPTED
|| r
== GNUTLS_E_AGAIN
)
1380 if (r
== GNUTLS_E_REHANDSHAKE
)
1382 fprintf (stderr
, "*** Received hello message\n");
1385 r
= gnutls_handshake (j
->tls_session
);
1387 while (r
== GNUTLS_E_INTERRUPTED
1388 || r
== GNUTLS_E_AGAIN
);
1394 ret
= gnutls_alert_send_appropriate
1395 (j
->tls_session
, r
);
1397 while (ret
== GNUTLS_E_AGAIN
1398 || ret
== GNUTLS_E_INTERRUPTED
);
1401 j
->http_state
= HTTP_STATE_CLOSING
;
1408 if (r
!= GNUTLS_E_UNEXPECTED_PACKET_LENGTH
)
1410 j
->http_state
= HTTP_STATE_CLOSING
;
1411 check_alert (j
->tls_session
, r
);
1413 "Error while receiving data\n");
1422 realloc (j
->http_request
, j
->request_length
+ r
+ 1);
1423 if (j
->http_request
!= NULL
)
1425 memcpy (j
->http_request
+ j
->request_length
, buf
, r
);
1426 j
->request_length
+= r
;
1427 j
->http_request
[j
->request_length
] = '\0';
1430 j
->http_state
= HTTP_STATE_CLOSING
;
1433 /* check if we have a full HTTP header */
1435 j
->http_response
= NULL
;
1436 if (j
->http_request
!= NULL
)
1438 if ((http
== 0 && strchr (j
->http_request
, '\n'))
1439 || strstr (j
->http_request
, "\r\n\r\n")
1440 || strstr (j
->http_request
, "\n\n"))
1442 get_response (j
->tls_session
, j
->http_request
,
1443 &j
->http_response
, &j
->response_length
);
1444 j
->http_state
= HTTP_STATE_RESPONSE
;
1445 j
->response_written
= 0;
1450 if (FD_ISSET (j
->fd
, &wr
))
1452 /* write partial response request */
1455 if (j
->handshake_ok
== 0)
1457 r
= gnutls_handshake (j
->tls_session
);
1458 if (r
< 0 && gnutls_error_is_fatal (r
) == 0)
1460 check_alert (j
->tls_session
, r
);
1463 else if (r
< 0 && gnutls_error_is_fatal (r
) == 1)
1467 j
->http_state
= HTTP_STATE_CLOSING
;
1468 check_alert (j
->tls_session
, r
);
1469 fprintf (stderr
, "Error in handshake\n");
1475 gnutls_alert_send_appropriate (j
->tls_session
, r
);
1477 while (ret
== GNUTLS_E_AGAIN
);
1481 if (gnutls_session_is_resumed (j
->tls_session
) != 0
1483 printf ("*** This is a resumed session\n");
1486 printf ("- connection from %s\n",
1487 human_addr ((struct sockaddr
*)
1488 &client_address
, calen
, topbuf
,
1491 print_info (j
->tls_session
, verbose
, verbose
);
1492 if (gnutls_auth_get_type (j
->tls_session
) ==
1493 GNUTLS_CRD_CERTIFICATE
)
1494 cert_verify (j
->tls_session
, NULL
);
1496 j
->handshake_ok
= 1;
1500 if (j
->handshake_ok
== 1 && j
->http_response
!= NULL
)
1502 /* FIXME if j->http_response == NULL? */
1503 r
= gnutls_record_send (j
->tls_session
,
1505 j
->response_written
,
1506 MIN (j
->response_length
-
1507 j
->response_written
,
1509 if (r
== GNUTLS_E_INTERRUPTED
|| r
== GNUTLS_E_AGAIN
)
1516 j
->http_state
= HTTP_STATE_CLOSING
;
1519 j
->http_state
= HTTP_STATE_REQUEST
;
1520 free (j
->http_response
);
1521 j
->response_length
= 0;
1522 j
->request_length
= 0;
1523 j
->http_request
[0] = 0;
1528 fprintf (stderr
, "Error while sending data\n");
1531 check_alert (j
->tls_session
, r
);
1535 j
->response_written
+= r
;
1536 /* check if we have written a complete response */
1537 if (j
->response_written
== j
->response_length
)
1540 j
->http_state
= HTTP_STATE_CLOSING
;
1543 j
->http_state
= HTTP_STATE_REQUEST
;
1544 free (j
->http_response
);
1545 j
->response_length
= 0;
1546 j
->request_length
= 0;
1547 j
->http_request
[0] = 0;
1554 j
->request_length
= 0;
1555 j
->http_request
[0] = 0;
1556 j
->http_state
= HTTP_STATE_REQUEST
;
1560 lloopend (listener_list
, j
);
1562 /* loop through all connections, closing those that are in error */
1563 lloopstart (listener_list
, j
)
1565 if (j
->http_state
== HTTP_STATE_CLOSING
)
1567 ldeleteinc (listener_list
, j
);
1570 lloopend (listener_list
, j
);
1574 gnutls_certificate_free_credentials (cert_cred
);
1578 gnutls_srp_free_server_credentials (srp_cred
);
1583 gnutls_psk_free_server_credentials (psk_cred
);
1587 gnutls_anon_free_server_credentials (dh_cred
);
1590 #ifdef ENABLE_SESSION_TICKET
1592 gnutls_free (session_ticket_key
.data
);
1597 gnutls_global_deinit ();
1602 cmd_parser (int argc
, char **argv
)
1604 optionProcess (&gnutls_servOptions
, argc
, argv
);
1606 disable_client_cert
= HAVE_OPT (DISABLE_CLIENT_CERT
);
1607 require_cert
= HAVE_OPT (REQUIRE_CLIENT_CERT
);
1608 if (HAVE_OPT (DEBUG
))
1609 debug
= OPT_VALUE_DEBUG
;
1611 if (HAVE_OPT (QUIET
))
1614 if (HAVE_OPT (PRIORITY
))
1615 priorities
= OPT_ARG (PRIORITY
);
1617 if (HAVE_OPT (LIST
))
1619 print_list (priorities
, verbose
);
1623 nodb
= HAVE_OPT (NODB
);
1624 noticket
= HAVE_OPT (NOTICKET
);
1626 if (HAVE_OPT (ECHO
))
1631 if (HAVE_OPT (X509FMTDER
))
1632 x509ctype
= GNUTLS_X509_FMT_DER
;
1634 x509ctype
= GNUTLS_X509_FMT_PEM
;
1636 generate
= HAVE_OPT (GENERATE
);
1638 if (HAVE_OPT (DHPARAMS
))
1639 dh_params_file
= OPT_ARG (DHPARAMS
);
1641 if (HAVE_OPT (X509KEYFILE
))
1642 x509_keyfile
= OPT_ARG (X509KEYFILE
);
1643 if (HAVE_OPT (X509CERTFILE
))
1644 x509_certfile
= OPT_ARG (X509CERTFILE
);
1646 if (HAVE_OPT (X509DSAKEYFILE
))
1647 x509_dsakeyfile
= OPT_ARG (X509DSAKEYFILE
);
1648 if (HAVE_OPT (X509DSACERTFILE
))
1649 x509_dsacertfile
= OPT_ARG (X509DSACERTFILE
);
1652 if (HAVE_OPT (X509ECCKEYFILE
))
1653 x509_ecckeyfile
= OPT_ARG (X509ECCKEYFILE
);
1654 if (HAVE_OPT (X509CERTFILE
))
1655 x509_ecccertfile
= OPT_ARG (X509ECCCERTFILE
);
1657 if (HAVE_OPT (X509CAFILE
))
1658 x509_cafile
= OPT_ARG (X509CAFILE
);
1659 if (HAVE_OPT (X509CRLFILE
))
1660 x509_crlfile
= OPT_ARG (X509CRLFILE
);
1662 if (HAVE_OPT (PGPKEYFILE
))
1663 pgp_keyfile
= OPT_ARG (PGPKEYFILE
);
1664 if (HAVE_OPT (PGPCERTFILE
))
1665 pgp_certfile
= OPT_ARG (PGPCERTFILE
);
1667 if (HAVE_OPT (PGPKEYRING
))
1668 pgp_keyring
= OPT_ARG (PGPKEYRING
);
1670 if (HAVE_OPT (SRPPASSWD
))
1671 srp_passwd
= OPT_ARG (SRPPASSWD
);
1672 if (HAVE_OPT (SRPPASSWDCONF
))
1673 srp_passwd_conf
= OPT_ARG (SRPPASSWDCONF
);
1675 if (HAVE_OPT (PSKPASSWD
))
1676 psk_passwd
= OPT_ARG (PSKPASSWD
);
1678 if (HAVE_OPT(OCSP_RESPONSE
))
1679 status_response_ocsp
= OPT_ARG(OCSP_RESPONSE
);
1683 /* session resuming support */
1685 #define SESSION_ID_SIZE 32
1686 #define SESSION_DATA_SIZE 1024
1690 char session_id
[SESSION_ID_SIZE
];
1691 unsigned int session_id_size
;
1693 char session_data
[SESSION_DATA_SIZE
];
1694 unsigned int session_data_size
;
1697 static CACHE
*cache_db
;
1698 int cache_db_ptr
= 0;
1703 /* allocate cache_db */
1704 cache_db
= calloc (1, ssl_session_cache
* sizeof (CACHE
));
1708 wrap_db_deinit (void)
1713 wrap_db_store (void *dbf
, gnutls_datum_t key
, gnutls_datum_t data
)
1716 if (cache_db
== NULL
)
1719 if (key
.size
> SESSION_ID_SIZE
)
1721 if (data
.size
> SESSION_DATA_SIZE
)
1724 memcpy (cache_db
[cache_db_ptr
].session_id
, key
.data
, key
.size
);
1725 cache_db
[cache_db_ptr
].session_id_size
= key
.size
;
1727 memcpy (cache_db
[cache_db_ptr
].session_data
, data
.data
, data
.size
);
1728 cache_db
[cache_db_ptr
].session_data_size
= data
.size
;
1731 cache_db_ptr
%= ssl_session_cache
;
1736 static gnutls_datum_t
1737 wrap_db_fetch (void *dbf
, gnutls_datum_t key
)
1739 gnutls_datum_t res
= { NULL
, 0 };
1742 if (cache_db
== NULL
)
1745 for (i
= 0; i
< ssl_session_cache
; i
++)
1747 if (key
.size
== cache_db
[i
].session_id_size
&&
1748 memcmp (key
.data
, cache_db
[i
].session_id
, key
.size
) == 0)
1750 res
.size
= cache_db
[i
].session_data_size
;
1752 res
.data
= gnutls_malloc (res
.size
);
1753 if (res
.data
== NULL
)
1756 memcpy (res
.data
, cache_db
[i
].session_data
, res
.size
);
1765 wrap_db_delete (void *dbf
, gnutls_datum_t key
)
1769 if (cache_db
== NULL
)
1772 for (i
= 0; i
< ssl_session_cache
; i
++)
1774 if (key
.size
== (unsigned int) cache_db
[i
].session_id_size
&&
1775 memcmp (key
.data
, cache_db
[i
].session_id
, key
.size
) == 0)
1778 cache_db
[i
].session_id_size
= 0;
1779 cache_db
[i
].session_data_size
= 0;