2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software
5 * Author: Simon Josefsson
7 * This file is part of GnuTLS.
9 * GnuTLS is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * GnuTLS is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with GnuTLS; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 /* Parts copied from GnuTLS example programs. */
33 #include <sys/types.h>
35 #include <netinet/in.h>
36 #include <sys/socket.h>
38 #include <arpa/inet.h>
41 #include <gnutls/gnutls.h>
45 #include "ex-session-info.c"
46 #include "ex-x509-info.c"
52 tls_log_func (int level
, const char *str
)
54 fprintf (stderr
, "%s |<%d>| %s", child
? "server" : "client", level
, str
);
57 /* A very basic TLS client, with anonymous authentication.
62 #define MSG "Hello TLS"
64 static unsigned char ca_pem
[] =
65 "-----BEGIN CERTIFICATE-----\n"
66 "MIIB5zCCAVKgAwIBAgIERiYdJzALBgkqhkiG9w0BAQUwGTEXMBUGA1UEAxMOR251\n"
67 "VExTIHRlc3QgQ0EwHhcNMDcwNDE4MTMyOTExWhcNMDgwNDE3MTMyOTExWjAZMRcw\n"
68 "FQYDVQQDEw5HbnVUTFMgdGVzdCBDQTCBnDALBgkqhkiG9w0BAQEDgYwAMIGIAoGA\n"
69 "vuyYeh1vfmslnuggeEKgZAVmQ5ltSdUY7H25WGSygKMUYZ0KT74v8C780qtcNt9T\n"
70 "7EPH/N6RvB4BprdssgcQLsthR3XKA84jbjjxNCcaGs33lvOz8A1nf8p3hD+cKfRi\n"
71 "kfYSW2JazLrtCC4yRCas/SPOUxu78of+3HiTfFm/oXUCAwEAAaNDMEEwDwYDVR0T\n"
72 "AQH/BAUwAwEB/zAPBgNVHQ8BAf8EBQMDBwQAMB0GA1UdDgQWBBTpPBz7rZJu5gak\n"
73 "Viyi4cBTJ8jylTALBgkqhkiG9w0BAQUDgYEAiaIRqGfp1jPpNeVhABK60SU0KIAy\n"
74 "njuu7kHq5peUgYn8Jd9zNzExBOEp1VOipGsf6G66oQAhDFp2o8zkz7ZH71zR4HEW\n"
75 "KoX6n5Emn6DvcEH/9pAhnGxNHJAoS7czTKv/JDZJhkqHxyrE1fuLsg5Qv25DTw7+\n"
76 "PfqUpIhz5Bbm7J4=\n" "-----END CERTIFICATE-----\n";
77 const gnutls_datum_t ca
= { ca_pem
, sizeof (ca_pem
) };
79 static unsigned char cert_pem
[] =
80 "-----BEGIN CERTIFICATE-----\n"
81 "MIICHjCCAYmgAwIBAgIERiYdNzALBgkqhkiG9w0BAQUwGTEXMBUGA1UEAxMOR251\n"
82 "VExTIHRlc3QgQ0EwHhcNMDcwNDE4MTMyOTI3WhcNMDgwNDE3MTMyOTI3WjAdMRsw\n"
83 "GQYDVQQDExJHbnVUTFMgdGVzdCBjbGllbnQwgZwwCwYJKoZIhvcNAQEBA4GMADCB\n"
84 "iAKBgLtmQ/Xyxde2jMzF3/WIO7HJS2oOoa0gUEAIgKFPXKPQ+GzP5jz37AR2ExeL\n"
85 "ZIkiW8DdU3w77XwEu4C5KL6Om8aOoKUSy/VXHqLnu7czSZ/ju0quak1o/8kR4jKN\n"
86 "zj2AC41179gAgY8oBAOgIo1hBAf6tjd9IQdJ0glhaZiQo1ipAgMBAAGjdjB0MAwG\n"
87 "A1UdEwEB/wQCMAAwEwYDVR0lBAwwCgYIKwYBBQUHAwIwDwYDVR0PAQH/BAUDAweg\n"
88 "ADAdBgNVHQ4EFgQUTLkKm/odNON+3svSBxX+odrLaJEwHwYDVR0jBBgwFoAU6Twc\n"
89 "+62SbuYGpFYsouHAUyfI8pUwCwYJKoZIhvcNAQEFA4GBALujmBJVZnvaTXr9cFRJ\n"
90 "jpfc/3X7sLUsMvumcDE01ls/cG5mIatmiyEU9qI3jbgUf82z23ON/acwJf875D3/\n"
91 "U7jyOsBJ44SEQITbin2yUeJMIm1tievvdNXBDfW95AM507ShzP12sfiJkJfjjdhy\n"
92 "dc8Siq5JojruiMizAf0pA7in\n" "-----END CERTIFICATE-----\n";
93 const gnutls_datum_t cert
= { cert_pem
, sizeof (cert_pem
) };
95 static unsigned char key_pem
[] =
96 "-----BEGIN RSA PRIVATE KEY-----\n"
97 "MIICXAIBAAKBgQC7ZkP18sXXtozMxd/1iDuxyUtqDqGtIFBACIChT1yj0Phsz+Y8\n"
98 "9+wEdhMXi2SJIlvA3VN8O+18BLuAuSi+jpvGjqClEsv1Vx6i57u3M0mf47tKrmpN\n"
99 "aP/JEeIyjc49gAuNde/YAIGPKAQDoCKNYQQH+rY3fSEHSdIJYWmYkKNYqQIDAQAB\n"
100 "AoGADpmARG5CQxS+AesNkGmpauepiCz1JBF/JwnyiX6vEzUh0Ypd39SZztwrDxvF\n"
101 "PJjQaKVljml1zkJpIDVsqvHdyVdse8M+Qn6hw4x2p5rogdvhhIL1mdWo7jWeVJTF\n"
102 "RKB7zLdMPs3ySdtcIQaF9nUAQ2KJEvldkO3m/bRJFEp54k0CQQDYy+RlTmwRD6hy\n"
103 "7UtMjR0H3CSZJeQ8svMCxHLmOluG9H1UKk55ZBYfRTsXniqUkJBZ5wuV1L+pR9EK\n"
104 "ca89a+1VAkEA3UmBelwEv2u9cAU1QjKjmwju1JgXbrjEohK+3B5y0ESEXPAwNQT9\n"
105 "TrDM1m9AyxYTWLxX93dI5QwNFJtmbtjeBQJARSCWXhsoaDRG8QZrCSjBxfzTCqZD\n"
106 "ZXtl807ymCipgJm60LiAt0JLr4LiucAsMZz6+j+quQbSakbFCACB8SLV1QJBAKZQ\n"
107 "YKf+EPNtnmta/rRKKvySsi3GQZZN+Dt3q0r094XgeTsAqrqujVNfPhTMeP4qEVBX\n"
108 "/iVX2cmMTSh3w3z8MaECQEp0XJWDVKOwcTW6Ajp9SowtmiZ3YDYo1LF9igb4iaLv\n"
109 "sWZGfbnU3ryjvkb6YuFjgtzbZDZHWQCo8/cOtOBmPdk=\n"
110 "-----END RSA PRIVATE KEY-----\n";
111 const gnutls_datum_t key
= { key_pem
, sizeof (key_pem
) };
117 gnutls_session_t session
;
118 char buffer
[MAX_BUF
+ 1];
119 gnutls_certificate_credentials_t xcred
;
121 gnutls_global_init ();
123 gnutls_global_set_log_function (tls_log_func
);
125 gnutls_global_set_log_level (6);
127 gnutls_certificate_allocate_credentials (&xcred
);
129 /* sets the trusted cas file
131 gnutls_certificate_set_x509_trust_mem (xcred
, &ca
, GNUTLS_X509_FMT_PEM
);
132 gnutls_certificate_set_x509_key_mem (xcred
, &cert
, &key
,
133 GNUTLS_X509_FMT_PEM
);
135 /* Initialize TLS session
137 gnutls_init (&session
, GNUTLS_CLIENT
);
139 /* Use default priorities */
140 gnutls_set_default_priority (session
);
142 /* put the x509 credentials to the current session
144 gnutls_credentials_set (session
, GNUTLS_CRD_CERTIFICATE
, xcred
);
146 /* connect to the peer
150 gnutls_transport_set_ptr (session
, (gnutls_transport_ptr_t
) sd
);
152 /* Perform the TLS handshake
154 ret
= gnutls_handshake (session
);
158 fail ("client: Handshake failed\n");
164 success ("client: Handshake was completed\n");
168 success ("client: TLS version is: %s\n",
169 gnutls_protocol_get_name (gnutls_protocol_get_version
172 /* see the Getting peer's information example */
174 print_info (session
);
176 ret
= gnutls_record_send (session
, MSG
, strlen (MSG
));
178 if (ret
== strlen (MSG
))
181 success ("client: sent record.\n");
185 fail ("client: failed to send record.\n");
190 ret
= gnutls_record_recv (session
, buffer
, MAX_BUF
);
193 success ("client: recv returned %d.\n", ret
);
195 if (ret
== GNUTLS_E_REHANDSHAKE
)
198 success ("client: doing handshake!\n");
199 ret
= gnutls_handshake (session
);
203 success ("client: handshake complete, reading again.\n");
204 ret
= gnutls_record_recv (session
, buffer
, MAX_BUF
);
208 fail ("client: handshake failed.\n");
215 success ("client: Peer has closed the TLS connection\n");
220 fail ("client: Error: %s\n", gnutls_strerror (ret
));
226 printf ("- Received %d bytes: ", ret
);
227 for (ii
= 0; ii
< ret
; ii
++)
229 fputc (buffer
[ii
], stdout
);
231 fputs ("\n", stdout
);
234 gnutls_bye (session
, GNUTLS_SHUT_RDWR
);
240 gnutls_deinit (session
);
242 gnutls_certificate_free_credentials (xcred
);
244 gnutls_global_deinit ();
247 /* This is a sample TLS 1.0 echo server, using X.509 authentication.
250 #define SA struct sockaddr
252 #define PORT 5556 /* listen to 5556 port */
255 /* These are global */
256 gnutls_certificate_credentials_t x509_cred
;
258 static gnutls_session_t
259 initialize_tls_session (void)
261 gnutls_session_t session
;
263 gnutls_init (&session
, GNUTLS_SERVER
);
265 /* avoid calling all the priority functions, since the defaults
268 gnutls_set_default_priority (session
);
270 gnutls_credentials_set (session
, GNUTLS_CRD_CERTIFICATE
, x509_cred
);
272 /* request client certificate if any.
273 Moved to later on to be able to test re-handshakes.
274 gnutls_certificate_server_set_request (session, GNUTLS_CERT_REQUEST);
277 gnutls_dh_set_prime_bits (session
, DH_BITS
);
282 static gnutls_dh_params_t dh_params
;
285 generate_dh_params (void)
287 const gnutls_datum_t p3
= { (char *) pkcs3
, strlen (pkcs3
) };
288 /* Generate Diffie-Hellman parameters - for use with DHE
289 * kx algorithms. These should be discarded and regenerated
290 * once a day, once a week or once a month. Depending on the
291 * security requirements.
293 gnutls_dh_params_init (&dh_params
);
294 return gnutls_dh_params_import_pkcs3 (dh_params
, &p3
, GNUTLS_X509_FMT_PEM
);
297 int err
, listen_sd
, i
;
299 struct sockaddr_in sa_serv
;
300 struct sockaddr_in sa_cli
;
303 gnutls_session_t session
;
304 char buffer
[MAX_BUF
+ 1];
308 static unsigned char server_cert_pem
[] =
309 "-----BEGIN CERTIFICATE-----\n"
310 "MIICVjCCAcGgAwIBAgIERiYdMTALBgkqhkiG9w0BAQUwGTEXMBUGA1UEAxMOR251\n"
311 "VExTIHRlc3QgQ0EwHhcNMDcwNDE4MTMyOTIxWhcNMDgwNDE3MTMyOTIxWjA3MRsw\n"
312 "GQYDVQQKExJHbnVUTFMgdGVzdCBzZXJ2ZXIxGDAWBgNVBAMTD3Rlc3QuZ251dGxz\n"
313 "Lm9yZzCBnDALBgkqhkiG9w0BAQEDgYwAMIGIAoGA17pcr6MM8C6pJ1aqU46o63+B\n"
314 "dUxrmL5K6rce+EvDasTaDQC46kwTHzYWk95y78akXrJutsoKiFV1kJbtple8DDt2\n"
315 "DZcevensf9Op7PuFZKBroEjOd35znDET/z3IrqVgbtm2jFqab7a+n2q9p/CgMyf1\n"
316 "tx2S5Zacc1LWn9bIjrECAwEAAaOBkzCBkDAMBgNVHRMBAf8EAjAAMBoGA1UdEQQT\n"
317 "MBGCD3Rlc3QuZ251dGxzLm9yZzATBgNVHSUEDDAKBggrBgEFBQcDATAPBgNVHQ8B\n"
318 "Af8EBQMDB6AAMB0GA1UdDgQWBBTrx0Vu5fglyoyNgw106YbU3VW0dTAfBgNVHSME\n"
319 "GDAWgBTpPBz7rZJu5gakViyi4cBTJ8jylTALBgkqhkiG9w0BAQUDgYEAaFEPTt+7\n"
320 "bzvBuOf7+QmeQcn29kT6Bsyh1RHJXf8KTk5QRfwp6ogbp94JQWcNQ/S7YDFHglD1\n"
321 "AwUNBRXwd3riUsMnsxgeSDxYBfJYbDLeohNBsqaPDJb7XailWbMQKfAbFQ8cnOxg\n"
322 "rOKLUQRWJ0K3HyXRMhbqjdLIaQiCvQLuizo=\n" "-----END CERTIFICATE-----\n";
324 const gnutls_datum_t server_cert
= { server_cert_pem
,
325 sizeof (server_cert_pem
)
328 static unsigned char server_key_pem
[] =
329 "-----BEGIN RSA PRIVATE KEY-----\n"
330 "MIICXAIBAAKBgQDXulyvowzwLqknVqpTjqjrf4F1TGuYvkrqtx74S8NqxNoNALjq\n"
331 "TBMfNhaT3nLvxqResm62ygqIVXWQlu2mV7wMO3YNlx696ex/06ns+4VkoGugSM53\n"
332 "fnOcMRP/PciupWBu2baMWppvtr6far2n8KAzJ/W3HZLllpxzUtaf1siOsQIDAQAB\n"
333 "AoGAYAFyKkAYC/PYF8e7+X+tsVCHXppp8AoP8TEZuUqOZz/AArVlle/ROrypg5kl\n"
334 "8YunrvUdzH9R/KZ7saNZlAPLjZyFG9beL/am6Ai7q7Ma5HMqjGU8kTEGwD7K+lbG\n"
335 "iomokKMOl+kkbY/2sI5Czmbm+/PqLXOjtVc5RAsdbgvtmvkCQQDdV5QuU8jap8Hs\n"
336 "Eodv/tLJ2z4+SKCV2k/7FXSKWe0vlrq0cl2qZfoTUYRnKRBcWxc9o92DxK44wgPi\n"
337 "oMQS+O7fAkEA+YG+K9e60sj1K4NYbMPAbYILbZxORDecvP8lcphvwkOVUqbmxOGh\n"
338 "XRmTZUuhBrJhJKKf6u7gf3KWlPl6ShKEbwJASC118cF6nurTjuLf7YKARDjNTEws\n"
339 "qZEeQbdWYINAmCMj0RH2P0mvybrsXSOD5UoDAyO7aWuqkHGcCLv6FGG+qwJAOVqq\n"
340 "tXdUucl6GjOKKw5geIvRRrQMhb/m5scb+5iw8A4LEEHPgGiBaF5NtJZLALgWfo5n\n"
341 "hmC8+G8F0F78znQtPwJBANexu+Tg5KfOnzSILJMo3oXiXhf5PqXIDmbN0BKyCKAQ\n"
342 "LfkcEcUbVfmDaHpvzwY9VEaoMOKVLitETXdNSxVpvWM=\n"
343 "-----END RSA PRIVATE KEY-----\n";
345 const gnutls_datum_t server_key
= { server_key_pem
,
346 sizeof (server_key_pem
)
354 listen_sd
= socket (AF_INET
, SOCK_STREAM
, 0);
358 fail ("server: socket failed\n");
362 memset (&sa_serv
, '\0', sizeof (sa_serv
));
363 sa_serv
.sin_family
= AF_INET
;
364 sa_serv
.sin_addr
.s_addr
= INADDR_ANY
;
365 sa_serv
.sin_port
= htons (PORT
); /* Server Port number */
367 setsockopt (listen_sd
, SOL_SOCKET
, SO_REUSEADDR
, (void *) &optval
,
370 err
= bind (listen_sd
, (SA
*) & sa_serv
, sizeof (sa_serv
));
374 fail ("server: bind failed\n");
378 err
= listen (listen_sd
, 1024);
382 fail ("server: listen failed\n");
387 success ("server: ready. Listening to port '%d'.\n", PORT
);
393 /* this must be called once in the program
395 gnutls_global_init ();
397 gnutls_global_set_log_function (tls_log_func
);
399 gnutls_global_set_log_level (6);
401 gnutls_certificate_allocate_credentials (&x509_cred
);
402 gnutls_certificate_set_x509_trust_mem (x509_cred
, &ca
, GNUTLS_X509_FMT_PEM
);
404 gnutls_certificate_set_x509_key_mem (x509_cred
, &server_cert
, &server_key
,
405 GNUTLS_X509_FMT_PEM
);
408 success ("Launched, generating DH parameters...\n");
410 generate_dh_params ();
412 gnutls_certificate_set_dh_params (x509_cred
, dh_params
);
414 client_len
= sizeof (sa_cli
);
416 session
= initialize_tls_session ();
418 sd
= accept (listen_sd
, (SA
*) & sa_cli
, &client_len
);
421 success ("server: connection from %s, port %d\n",
422 inet_ntop (AF_INET
, &sa_cli
.sin_addr
, topbuf
,
423 sizeof (topbuf
)), ntohs (sa_cli
.sin_port
));
425 gnutls_transport_set_ptr (session
, (gnutls_transport_ptr_t
) sd
);
426 ret
= gnutls_handshake (session
);
430 gnutls_deinit (session
);
431 fail ("server: Handshake has failed (%s)\n\n", gnutls_strerror (ret
));
436 success ("server: Handshake was completed\n");
437 success ("server: TLS version is: %s\n",
438 gnutls_protocol_get_name (gnutls_protocol_get_version
442 /* see the Getting peer's information example */
444 print_info (session
);
449 memset (buffer
, 0, MAX_BUF
+ 1);
450 ret
= gnutls_record_recv (session
, buffer
, MAX_BUF
);
455 success ("server: Peer has closed the GnuTLS connection\n");
460 fail ("server: Received corrupted data(%d). Closing...\n", ret
);
465 gnutls_certificate_server_set_request (session
,
466 GNUTLS_CERT_REQUEST
);
469 success ("server: got data, forcing rehandshake.\n");
471 ret
= gnutls_rehandshake (session
);
474 fail ("server: rehandshake failed\n");
479 ret
= gnutls_handshake (session
);
482 fail ("server: (re)handshake failed\n");
488 success ("server: rehandshake complete.\n");
490 /* echo data back to the client
492 gnutls_record_send (session
, buffer
, strlen (buffer
));
495 /* do not wait for the peer to close the connection.
497 gnutls_bye (session
, GNUTLS_SHUT_WR
);
500 gnutls_deinit (session
);
504 gnutls_certificate_free_credentials (x509_cred
);
506 gnutls_dh_params_deinit (dh_params
);
508 gnutls_global_deinit ();
511 success ("server: finished\n");