Generated.
[gnutls.git] / tests / x509self.c
bloba29809b9b7c79f3310bb73571d9357ec7c9d0271
1 /*
2 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation
4 * Author: Simon Josefsson
6 * This file is part of GNUTLS.
8 * GNUTLS is free software; you can redistribute it and/or modify it
9 * 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, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with GNUTLS; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 /* Parts copied from GnuTLS example programs. */
25 #if HAVE_CONFIG_H
26 # include <config.h>
27 #endif
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #include <netinet/in.h>
34 #include <sys/socket.h>
35 #include <sys/wait.h>
36 #include <arpa/inet.h>
37 #include <unistd.h>
38 #include <gnutls/gnutls.h>
40 #include "utils.h"
42 #include "ex-session-info.c"
43 #include "ex-x509-info.c"
45 pid_t child;
47 static void
48 tls_log_func (int level, const char *str)
50 fprintf (stderr, "%s |<%d>| %s", child ? "server" : "client", level, str);
53 /* A very basic TLS client, with anonymous authentication.
56 #define MAX_BUF 1024
57 #define MSG "Hello TLS"
59 /* Connects to the peer and returns a socket
60 * descriptor.
62 int
63 tcp_connect (void)
65 const char *PORT = "5556";
66 const char *SERVER = "127.0.0.1";
67 int err, sd;
68 struct sockaddr_in sa;
70 /* connects to server
72 sd = socket (AF_INET, SOCK_STREAM, 0);
74 memset (&sa, '\0', sizeof (sa));
75 sa.sin_family = AF_INET;
76 sa.sin_port = htons (atoi (PORT));
77 inet_pton (AF_INET, SERVER, &sa.sin_addr);
79 err = connect (sd, (struct sockaddr *) &sa, sizeof (sa));
80 if (err < 0)
82 fprintf (stderr, "Connect error\n");
83 exit (1);
86 return sd;
89 /* closes the given socket descriptor.
91 void
92 tcp_close (int sd)
94 shutdown (sd, SHUT_RDWR); /* no more receptions */
95 close (sd);
98 const char ca_pem[] =
99 "-----BEGIN CERTIFICATE-----\n"
100 "MIIB5zCCAVKgAwIBAgIERiYdJzALBgkqhkiG9w0BAQUwGTEXMBUGA1UEAxMOR251\n"
101 "VExTIHRlc3QgQ0EwHhcNMDcwNDE4MTMyOTExWhcNMDgwNDE3MTMyOTExWjAZMRcw\n"
102 "FQYDVQQDEw5HbnVUTFMgdGVzdCBDQTCBnDALBgkqhkiG9w0BAQEDgYwAMIGIAoGA\n"
103 "vuyYeh1vfmslnuggeEKgZAVmQ5ltSdUY7H25WGSygKMUYZ0KT74v8C780qtcNt9T\n"
104 "7EPH/N6RvB4BprdssgcQLsthR3XKA84jbjjxNCcaGs33lvOz8A1nf8p3hD+cKfRi\n"
105 "kfYSW2JazLrtCC4yRCas/SPOUxu78of+3HiTfFm/oXUCAwEAAaNDMEEwDwYDVR0T\n"
106 "AQH/BAUwAwEB/zAPBgNVHQ8BAf8EBQMDBwQAMB0GA1UdDgQWBBTpPBz7rZJu5gak\n"
107 "Viyi4cBTJ8jylTALBgkqhkiG9w0BAQUDgYEAiaIRqGfp1jPpNeVhABK60SU0KIAy\n"
108 "njuu7kHq5peUgYn8Jd9zNzExBOEp1VOipGsf6G66oQAhDFp2o8zkz7ZH71zR4HEW\n"
109 "KoX6n5Emn6DvcEH/9pAhnGxNHJAoS7czTKv/JDZJhkqHxyrE1fuLsg5Qv25DTw7+\n"
110 "PfqUpIhz5Bbm7J4=\n"
111 "-----END CERTIFICATE-----\n";
112 const gnutls_datum_t ca = { ca_pem, sizeof (ca_pem) };
114 const char cert_pem[] =
115 "-----BEGIN CERTIFICATE-----\n"
116 "MIICHjCCAYmgAwIBAgIERiYdNzALBgkqhkiG9w0BAQUwGTEXMBUGA1UEAxMOR251\n"
117 "VExTIHRlc3QgQ0EwHhcNMDcwNDE4MTMyOTI3WhcNMDgwNDE3MTMyOTI3WjAdMRsw\n"
118 "GQYDVQQDExJHbnVUTFMgdGVzdCBjbGllbnQwgZwwCwYJKoZIhvcNAQEBA4GMADCB\n"
119 "iAKBgLtmQ/Xyxde2jMzF3/WIO7HJS2oOoa0gUEAIgKFPXKPQ+GzP5jz37AR2ExeL\n"
120 "ZIkiW8DdU3w77XwEu4C5KL6Om8aOoKUSy/VXHqLnu7czSZ/ju0quak1o/8kR4jKN\n"
121 "zj2AC41179gAgY8oBAOgIo1hBAf6tjd9IQdJ0glhaZiQo1ipAgMBAAGjdjB0MAwG\n"
122 "A1UdEwEB/wQCMAAwEwYDVR0lBAwwCgYIKwYBBQUHAwIwDwYDVR0PAQH/BAUDAweg\n"
123 "ADAdBgNVHQ4EFgQUTLkKm/odNON+3svSBxX+odrLaJEwHwYDVR0jBBgwFoAU6Twc\n"
124 "+62SbuYGpFYsouHAUyfI8pUwCwYJKoZIhvcNAQEFA4GBALujmBJVZnvaTXr9cFRJ\n"
125 "jpfc/3X7sLUsMvumcDE01ls/cG5mIatmiyEU9qI3jbgUf82z23ON/acwJf875D3/\n"
126 "U7jyOsBJ44SEQITbin2yUeJMIm1tievvdNXBDfW95AM507ShzP12sfiJkJfjjdhy\n"
127 "dc8Siq5JojruiMizAf0pA7in\n"
128 "-----END CERTIFICATE-----\n";
129 const gnutls_datum_t cert = { cert_pem, sizeof (cert_pem) };
131 const char key_pem[] =
132 "-----BEGIN RSA PRIVATE KEY-----\n"
133 "MIICXAIBAAKBgQC7ZkP18sXXtozMxd/1iDuxyUtqDqGtIFBACIChT1yj0Phsz+Y8\n"
134 "9+wEdhMXi2SJIlvA3VN8O+18BLuAuSi+jpvGjqClEsv1Vx6i57u3M0mf47tKrmpN\n"
135 "aP/JEeIyjc49gAuNde/YAIGPKAQDoCKNYQQH+rY3fSEHSdIJYWmYkKNYqQIDAQAB\n"
136 "AoGADpmARG5CQxS+AesNkGmpauepiCz1JBF/JwnyiX6vEzUh0Ypd39SZztwrDxvF\n"
137 "PJjQaKVljml1zkJpIDVsqvHdyVdse8M+Qn6hw4x2p5rogdvhhIL1mdWo7jWeVJTF\n"
138 "RKB7zLdMPs3ySdtcIQaF9nUAQ2KJEvldkO3m/bRJFEp54k0CQQDYy+RlTmwRD6hy\n"
139 "7UtMjR0H3CSZJeQ8svMCxHLmOluG9H1UKk55ZBYfRTsXniqUkJBZ5wuV1L+pR9EK\n"
140 "ca89a+1VAkEA3UmBelwEv2u9cAU1QjKjmwju1JgXbrjEohK+3B5y0ESEXPAwNQT9\n"
141 "TrDM1m9AyxYTWLxX93dI5QwNFJtmbtjeBQJARSCWXhsoaDRG8QZrCSjBxfzTCqZD\n"
142 "ZXtl807ymCipgJm60LiAt0JLr4LiucAsMZz6+j+quQbSakbFCACB8SLV1QJBAKZQ\n"
143 "YKf+EPNtnmta/rRKKvySsi3GQZZN+Dt3q0r094XgeTsAqrqujVNfPhTMeP4qEVBX\n"
144 "/iVX2cmMTSh3w3z8MaECQEp0XJWDVKOwcTW6Ajp9SowtmiZ3YDYo1LF9igb4iaLv\n"
145 "sWZGfbnU3ryjvkb6YuFjgtzbZDZHWQCo8/cOtOBmPdk=\n"
146 "-----END RSA PRIVATE KEY-----\n";
147 const gnutls_datum_t key = { key_pem, sizeof (key_pem) };
149 void
150 client (void)
152 int ret, sd, ii;
153 gnutls_session_t session;
154 char buffer[MAX_BUF + 1];
155 gnutls_certificate_credentials_t xcred;
157 gnutls_global_init ();
159 gnutls_global_set_log_function (tls_log_func);
160 if (debug)
161 gnutls_global_set_log_level (4711);
163 gnutls_certificate_allocate_credentials (&xcred);
165 /* sets the trusted cas file
167 gnutls_certificate_set_x509_trust_mem (xcred, &ca, GNUTLS_X509_FMT_PEM);
168 gnutls_certificate_set_x509_key_mem (xcred, &cert, &key,
169 GNUTLS_X509_FMT_PEM);
171 /* Initialize TLS session
173 gnutls_init (&session, GNUTLS_CLIENT);
175 /* Use default priorities */
176 gnutls_set_default_priority (session);
178 /* put the x509 credentials to the current session
180 gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, xcred);
182 /* connect to the peer
184 sd = tcp_connect ();
186 gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
188 /* Perform the TLS handshake
190 ret = gnutls_handshake (session);
192 if (ret < 0)
194 fail ("client: Handshake failed\n");
195 gnutls_perror (ret);
196 goto end;
198 else
200 success ("client: Handshake was completed\n");
203 success ("client: TLS version is: %s\n",
204 gnutls_protocol_get_name (gnutls_protocol_get_version (session)));
206 /* see the Getting peer's information example */
207 print_info(session);
209 gnutls_record_send (session, MSG, strlen (MSG));
211 ret = gnutls_record_recv (session, buffer, MAX_BUF);
212 if (ret == 0)
214 success ("client: Peer has closed the TLS connection\n");
215 goto end;
217 else if (ret < 0)
219 fail ("client: Error: %s\n", gnutls_strerror (ret));
220 goto end;
223 printf ("- Received %d bytes: ", ret);
224 for (ii = 0; ii < ret; ii++)
226 fputc (buffer[ii], stdout);
228 fputs ("\n", stdout);
230 gnutls_bye (session, GNUTLS_SHUT_RDWR);
232 end:
234 tcp_close (sd);
236 gnutls_deinit (session);
238 gnutls_certificate_free_credentials (xcred);
240 gnutls_global_deinit ();
243 /* This is a sample TLS 1.0 echo server, using X.509 authentication.
246 #define SA struct sockaddr
247 #define MAX_BUF 1024
248 #define PORT 5556 /* listen to 5556 port */
249 #define DH_BITS 1024
251 /* These are global */
252 gnutls_certificate_credentials_t x509_cred;
254 gnutls_session_t
255 initialize_tls_session (void)
257 gnutls_session_t session;
259 gnutls_init (&session, GNUTLS_SERVER);
261 /* avoid calling all the priority functions, since the defaults
262 * are adequate.
264 gnutls_set_default_priority (session);
266 gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, x509_cred);
268 /* request client certificate if any.
270 gnutls_certificate_server_set_request (session, GNUTLS_CERT_REQUEST);
272 gnutls_dh_set_prime_bits (session, DH_BITS);
274 return session;
277 static gnutls_dh_params_t dh_params;
279 static int
280 generate_dh_params (void)
282 const gnutls_datum_t p3 = { pkcs3, strlen (pkcs3) };
283 /* Generate Diffie Hellman parameters - for use with DHE
284 * kx algorithms. These should be discarded and regenerated
285 * once a day, once a week or once a month. Depending on the
286 * security requirements.
288 gnutls_dh_params_init (&dh_params);
289 return gnutls_dh_params_import_pkcs3 (dh_params, &p3, GNUTLS_X509_FMT_PEM);
292 int err, listen_sd, i;
293 int sd, ret;
294 struct sockaddr_in sa_serv;
295 struct sockaddr_in sa_cli;
296 int client_len;
297 char topbuf[512];
298 gnutls_session_t session;
299 char buffer[MAX_BUF + 1];
300 int optval = 1;
303 const char server_cert_pem[] =
304 "-----BEGIN CERTIFICATE-----\n"
305 "MIICVjCCAcGgAwIBAgIERiYdMTALBgkqhkiG9w0BAQUwGTEXMBUGA1UEAxMOR251\n"
306 "VExTIHRlc3QgQ0EwHhcNMDcwNDE4MTMyOTIxWhcNMDgwNDE3MTMyOTIxWjA3MRsw\n"
307 "GQYDVQQKExJHbnVUTFMgdGVzdCBzZXJ2ZXIxGDAWBgNVBAMTD3Rlc3QuZ251dGxz\n"
308 "Lm9yZzCBnDALBgkqhkiG9w0BAQEDgYwAMIGIAoGA17pcr6MM8C6pJ1aqU46o63+B\n"
309 "dUxrmL5K6rce+EvDasTaDQC46kwTHzYWk95y78akXrJutsoKiFV1kJbtple8DDt2\n"
310 "DZcevensf9Op7PuFZKBroEjOd35znDET/z3IrqVgbtm2jFqab7a+n2q9p/CgMyf1\n"
311 "tx2S5Zacc1LWn9bIjrECAwEAAaOBkzCBkDAMBgNVHRMBAf8EAjAAMBoGA1UdEQQT\n"
312 "MBGCD3Rlc3QuZ251dGxzLm9yZzATBgNVHSUEDDAKBggrBgEFBQcDATAPBgNVHQ8B\n"
313 "Af8EBQMDB6AAMB0GA1UdDgQWBBTrx0Vu5fglyoyNgw106YbU3VW0dTAfBgNVHSME\n"
314 "GDAWgBTpPBz7rZJu5gakViyi4cBTJ8jylTALBgkqhkiG9w0BAQUDgYEAaFEPTt+7\n"
315 "bzvBuOf7+QmeQcn29kT6Bsyh1RHJXf8KTk5QRfwp6ogbp94JQWcNQ/S7YDFHglD1\n"
316 "AwUNBRXwd3riUsMnsxgeSDxYBfJYbDLeohNBsqaPDJb7XailWbMQKfAbFQ8cnOxg\n"
317 "rOKLUQRWJ0K3HyXRMhbqjdLIaQiCvQLuizo=\n"
318 "-----END CERTIFICATE-----\n";
320 const gnutls_datum_t server_cert = { server_cert_pem,
321 sizeof (server_cert_pem) };
323 const char server_key_pem[] =
324 "-----BEGIN RSA PRIVATE KEY-----\n"
325 "MIICXAIBAAKBgQDXulyvowzwLqknVqpTjqjrf4F1TGuYvkrqtx74S8NqxNoNALjq\n"
326 "TBMfNhaT3nLvxqResm62ygqIVXWQlu2mV7wMO3YNlx696ex/06ns+4VkoGugSM53\n"
327 "fnOcMRP/PciupWBu2baMWppvtr6far2n8KAzJ/W3HZLllpxzUtaf1siOsQIDAQAB\n"
328 "AoGAYAFyKkAYC/PYF8e7+X+tsVCHXppp8AoP8TEZuUqOZz/AArVlle/ROrypg5kl\n"
329 "8YunrvUdzH9R/KZ7saNZlAPLjZyFG9beL/am6Ai7q7Ma5HMqjGU8kTEGwD7K+lbG\n"
330 "iomokKMOl+kkbY/2sI5Czmbm+/PqLXOjtVc5RAsdbgvtmvkCQQDdV5QuU8jap8Hs\n"
331 "Eodv/tLJ2z4+SKCV2k/7FXSKWe0vlrq0cl2qZfoTUYRnKRBcWxc9o92DxK44wgPi\n"
332 "oMQS+O7fAkEA+YG+K9e60sj1K4NYbMPAbYILbZxORDecvP8lcphvwkOVUqbmxOGh\n"
333 "XRmTZUuhBrJhJKKf6u7gf3KWlPl6ShKEbwJASC118cF6nurTjuLf7YKARDjNTEws\n"
334 "qZEeQbdWYINAmCMj0RH2P0mvybrsXSOD5UoDAyO7aWuqkHGcCLv6FGG+qwJAOVqq\n"
335 "tXdUucl6GjOKKw5geIvRRrQMhb/m5scb+5iw8A4LEEHPgGiBaF5NtJZLALgWfo5n\n"
336 "hmC8+G8F0F78znQtPwJBANexu+Tg5KfOnzSILJMo3oXiXhf5PqXIDmbN0BKyCKAQ\n"
337 "LfkcEcUbVfmDaHpvzwY9VEaoMOKVLitETXdNSxVpvWM=\n"
338 "-----END RSA PRIVATE KEY-----\n";
340 const gnutls_datum_t server_key = { server_key_pem,
341 sizeof (server_key_pem) };
343 void
344 server_start (void)
346 /* Socket operations
348 listen_sd = socket (AF_INET, SOCK_STREAM, 0);
349 if (err == -1)
351 perror ("socket");
352 fail ("server: socket failed\n");
353 return;
356 memset (&sa_serv, '\0', sizeof (sa_serv));
357 sa_serv.sin_family = AF_INET;
358 sa_serv.sin_addr.s_addr = INADDR_ANY;
359 sa_serv.sin_port = htons (PORT); /* Server Port number */
361 setsockopt (listen_sd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof (int));
363 err = bind (listen_sd, (SA *) & sa_serv, sizeof (sa_serv));
364 if (err == -1)
366 perror ("bind");
367 fail ("server: bind failed\n");
368 return;
371 err = listen (listen_sd, 1024);
372 if (err == -1)
374 perror ("listen");
375 fail ("server: listen failed\n");
376 return;
379 success ("server: ready. Listening to port '%d'.\n", PORT);
382 void
383 server (void)
385 /* this must be called once in the program
387 gnutls_global_init ();
389 gnutls_global_set_log_function (tls_log_func);
390 if (debug)
391 gnutls_global_set_log_level (4711);
393 gnutls_certificate_allocate_credentials (&x509_cred);
394 gnutls_certificate_set_x509_trust_mem (x509_cred, &ca, GNUTLS_X509_FMT_PEM);
396 gnutls_certificate_set_x509_key_mem (x509_cred, &server_cert, &server_key,
397 GNUTLS_X509_FMT_PEM);
399 success ("Launched, generating DH parameters...\n");
401 generate_dh_params ();
403 gnutls_certificate_set_dh_params (x509_cred, dh_params);
405 client_len = sizeof (sa_cli);
407 session = initialize_tls_session ();
409 sd = accept (listen_sd, (SA *) & sa_cli, &client_len);
411 success ("server: connection from %s, port %d\n",
412 inet_ntop (AF_INET, &sa_cli.sin_addr, topbuf,
413 sizeof (topbuf)), ntohs (sa_cli.sin_port));
415 gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
416 ret = gnutls_handshake (session);
417 if (ret < 0)
419 close (sd);
420 gnutls_deinit (session);
421 fail ("server: Handshake has failed (%s)\n\n", gnutls_strerror (ret));
422 return;
424 success ("server: Handshake was completed\n");
426 success ("server: TLS version is: %s\n",
427 gnutls_protocol_get_name (gnutls_protocol_get_version (session)));
429 /* see the Getting peer's information example */
430 print_info(session);
432 i = 0;
433 for (;;)
435 bzero (buffer, MAX_BUF + 1);
436 ret = gnutls_record_recv (session, buffer, MAX_BUF);
438 if (ret == 0)
440 success ("server: Peer has closed the GNUTLS connection\n");
441 break;
443 else if (ret < 0)
445 fail ("server: Received corrupted data(%d). Closing...\n", ret);
446 break;
448 else if (ret > 0)
450 /* echo data back to the client
452 gnutls_record_send (session, buffer, strlen (buffer));
455 /* do not wait for the peer to close the connection.
457 gnutls_bye (session, GNUTLS_SHUT_WR);
459 close (sd);
460 gnutls_deinit (session);
462 close (listen_sd);
464 gnutls_certificate_free_credentials (x509_cred);
466 gnutls_dh_params_deinit (dh_params);
468 gnutls_global_deinit ();
470 success ("server: finished\n");
474 void
475 doit (void)
477 /* parent */
478 server_start ();
479 if (error_count)
480 return;
482 child = fork ();
483 if (child < 0)
485 perror ("fork");
486 fail ("fork");
487 return;
490 if (child)
492 int status;
494 server ();
495 wait (&status);
497 else
498 client ();