corrected typos
[gnutls.git] / tests / x509dn.c
bloba6ae6f63ad2df0df136e83f69d14f5a4e2ae4ee4
1 /*
2 * Copyright (C) 2004-2012 Free Software Foundation, Inc.
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 #ifdef 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 <sys/socket.h>
34 #if !defined(_WIN32)
35 # include <netinet/in.h>
36 # include <sys/wait.h>
37 # include <arpa/inet.h>
38 #endif
39 #include <unistd.h>
40 #include <gnutls/gnutls.h>
41 #include <gnutls/abstract.h>
43 #include "utils.h"
45 #include "ex-session-info.c"
46 #include "ex-x509-info.c"
47 #include "tcp.c"
49 pid_t child;
51 static void
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.
60 #define MAX_BUF 1024
61 #define MSG "Hello TLS"
63 static unsigned char ca_pem[] =
64 "-----BEGIN CERTIFICATE-----\n"
65 "MIIB5zCCAVKgAwIBAgIERiYdJzALBgkqhkiG9w0BAQUwGTEXMBUGA1UEAxMOR251\n"
66 "VExTIHRlc3QgQ0EwHhcNMDcwNDE4MTMyOTExWhcNMDgwNDE3MTMyOTExWjAZMRcw\n"
67 "FQYDVQQDEw5HbnVUTFMgdGVzdCBDQTCBnDALBgkqhkiG9w0BAQEDgYwAMIGIAoGA\n"
68 "vuyYeh1vfmslnuggeEKgZAVmQ5ltSdUY7H25WGSygKMUYZ0KT74v8C780qtcNt9T\n"
69 "7EPH/N6RvB4BprdssgcQLsthR3XKA84jbjjxNCcaGs33lvOz8A1nf8p3hD+cKfRi\n"
70 "kfYSW2JazLrtCC4yRCas/SPOUxu78of+3HiTfFm/oXUCAwEAAaNDMEEwDwYDVR0T\n"
71 "AQH/BAUwAwEB/zAPBgNVHQ8BAf8EBQMDBwQAMB0GA1UdDgQWBBTpPBz7rZJu5gak\n"
72 "Viyi4cBTJ8jylTALBgkqhkiG9w0BAQUDgYEAiaIRqGfp1jPpNeVhABK60SU0KIAy\n"
73 "njuu7kHq5peUgYn8Jd9zNzExBOEp1VOipGsf6G66oQAhDFp2o8zkz7ZH71zR4HEW\n"
74 "KoX6n5Emn6DvcEH/9pAhnGxNHJAoS7czTKv/JDZJhkqHxyrE1fuLsg5Qv25DTw7+\n"
75 "PfqUpIhz5Bbm7J4=\n" "-----END CERTIFICATE-----\n";
76 const gnutls_datum_t ca = { ca_pem, sizeof (ca_pem) };
78 static unsigned char cert_pem[] =
79 "-----BEGIN CERTIFICATE-----\n"
80 "MIICHjCCAYmgAwIBAgIERiYdNzALBgkqhkiG9w0BAQUwGTEXMBUGA1UEAxMOR251\n"
81 "VExTIHRlc3QgQ0EwHhcNMDcwNDE4MTMyOTI3WhcNMDgwNDE3MTMyOTI3WjAdMRsw\n"
82 "GQYDVQQDExJHbnVUTFMgdGVzdCBjbGllbnQwgZwwCwYJKoZIhvcNAQEBA4GMADCB\n"
83 "iAKBgLtmQ/Xyxde2jMzF3/WIO7HJS2oOoa0gUEAIgKFPXKPQ+GzP5jz37AR2ExeL\n"
84 "ZIkiW8DdU3w77XwEu4C5KL6Om8aOoKUSy/VXHqLnu7czSZ/ju0quak1o/8kR4jKN\n"
85 "zj2AC41179gAgY8oBAOgIo1hBAf6tjd9IQdJ0glhaZiQo1ipAgMBAAGjdjB0MAwG\n"
86 "A1UdEwEB/wQCMAAwEwYDVR0lBAwwCgYIKwYBBQUHAwIwDwYDVR0PAQH/BAUDAweg\n"
87 "ADAdBgNVHQ4EFgQUTLkKm/odNON+3svSBxX+odrLaJEwHwYDVR0jBBgwFoAU6Twc\n"
88 "+62SbuYGpFYsouHAUyfI8pUwCwYJKoZIhvcNAQEFA4GBALujmBJVZnvaTXr9cFRJ\n"
89 "jpfc/3X7sLUsMvumcDE01ls/cG5mIatmiyEU9qI3jbgUf82z23ON/acwJf875D3/\n"
90 "U7jyOsBJ44SEQITbin2yUeJMIm1tievvdNXBDfW95AM507ShzP12sfiJkJfjjdhy\n"
91 "dc8Siq5JojruiMizAf0pA7in\n" "-----END CERTIFICATE-----\n";
92 const gnutls_datum_t cert = { cert_pem, sizeof (cert_pem) };
94 static unsigned char key_pem[] =
95 "-----BEGIN RSA PRIVATE KEY-----\n"
96 "MIICXAIBAAKBgQC7ZkP18sXXtozMxd/1iDuxyUtqDqGtIFBACIChT1yj0Phsz+Y8\n"
97 "9+wEdhMXi2SJIlvA3VN8O+18BLuAuSi+jpvGjqClEsv1Vx6i57u3M0mf47tKrmpN\n"
98 "aP/JEeIyjc49gAuNde/YAIGPKAQDoCKNYQQH+rY3fSEHSdIJYWmYkKNYqQIDAQAB\n"
99 "AoGADpmARG5CQxS+AesNkGmpauepiCz1JBF/JwnyiX6vEzUh0Ypd39SZztwrDxvF\n"
100 "PJjQaKVljml1zkJpIDVsqvHdyVdse8M+Qn6hw4x2p5rogdvhhIL1mdWo7jWeVJTF\n"
101 "RKB7zLdMPs3ySdtcIQaF9nUAQ2KJEvldkO3m/bRJFEp54k0CQQDYy+RlTmwRD6hy\n"
102 "7UtMjR0H3CSZJeQ8svMCxHLmOluG9H1UKk55ZBYfRTsXniqUkJBZ5wuV1L+pR9EK\n"
103 "ca89a+1VAkEA3UmBelwEv2u9cAU1QjKjmwju1JgXbrjEohK+3B5y0ESEXPAwNQT9\n"
104 "TrDM1m9AyxYTWLxX93dI5QwNFJtmbtjeBQJARSCWXhsoaDRG8QZrCSjBxfzTCqZD\n"
105 "ZXtl807ymCipgJm60LiAt0JLr4LiucAsMZz6+j+quQbSakbFCACB8SLV1QJBAKZQ\n"
106 "YKf+EPNtnmta/rRKKvySsi3GQZZN+Dt3q0r094XgeTsAqrqujVNfPhTMeP4qEVBX\n"
107 "/iVX2cmMTSh3w3z8MaECQEp0XJWDVKOwcTW6Ajp9SowtmiZ3YDYo1LF9igb4iaLv\n"
108 "sWZGfbnU3ryjvkb6YuFjgtzbZDZHWQCo8/cOtOBmPdk=\n"
109 "-----END RSA PRIVATE KEY-----\n";
110 const gnutls_datum_t key = { key_pem, sizeof (key_pem) };
112 #define EXPECT_RDN0 "GnuTLS test CA"
114 static int
115 cert_callback (gnutls_session_t session,
116 const gnutls_datum_t * req_ca_rdn, int nreqs,
117 const gnutls_pk_algorithm_t * sign_algos,
118 int sign_algos_length, gnutls_pcert_st ** pcert,
119 unsigned int* pcert_length, gnutls_privkey_t *pkey)
121 int result;
122 gnutls_x509_dn_t dn;
124 if (nreqs != 1)
126 fail ("client: invoked to provide client cert, %d CA .\n", nreqs);
127 return -1;
130 if (debug)
131 success ("client: invoked to provide client cert.\n");
133 result = gnutls_x509_dn_init (&dn);
134 if (result < 0)
136 fail ("client: could not initialize DN.\n");
137 return -1;
140 result = gnutls_x509_dn_import (dn, req_ca_rdn);
141 if (result == 0)
143 gnutls_x509_ava_st val;
145 if (debug)
146 success ("client: imported DN.\n");
148 if (gnutls_x509_dn_get_rdn_ava (dn, 0, 0, &val) == 0)
150 if (debug)
151 success ("client: got RDN 0.\n");
153 if (val.value.size == strlen (EXPECT_RDN0)
154 && strncmp ((char*)val.value.data, EXPECT_RDN0, val.value.size) == 0)
156 if (debug)
157 success ("client: RND 0 correct.\n");
159 else
161 fail ("client: RND 0 bad: %.*s\n",
162 val.value.size, val.value.data);
163 return -1;
166 else
168 fail ("client: could not retrieve RDN 0.\n");
169 return -1;
172 gnutls_x509_dn_deinit (dn);
174 else
176 fail ("client: failed to parse RDN: %s\n", gnutls_strerror (result));
179 return 0;
183 static void
184 client (void)
186 int ret, sd, ii;
187 gnutls_session_t session;
188 char buffer[MAX_BUF + 1];
189 gnutls_certificate_credentials_t xcred;
191 gnutls_global_init ();
193 gnutls_global_set_log_function (tls_log_func);
194 if (debug)
195 gnutls_global_set_log_level (4711);
197 gnutls_certificate_allocate_credentials (&xcred);
199 /* sets the trusted cas file
201 gnutls_certificate_set_x509_trust_mem (xcred, &ca, GNUTLS_X509_FMT_PEM);
203 gnutls_certificate_set_retrieve_function2 (xcred, cert_callback);
205 /* Initialize TLS session
207 gnutls_init (&session, GNUTLS_CLIENT);
209 /* Use default priorities */
210 gnutls_set_default_priority (session);
212 /* put the x509 credentials to the current session
214 gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, xcred);
216 /* connect to the peer
218 sd = tcp_connect ();
220 gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
222 /* Perform the TLS handshake
224 ret = gnutls_handshake (session);
226 if (ret < 0)
228 fail ("client: Handshake failed\n");
229 gnutls_perror (ret);
230 goto end;
232 else
234 if (debug)
235 success ("client: Handshake was completed\n");
238 if (debug)
239 success ("client: TLS version is: %s\n",
240 gnutls_protocol_get_name (gnutls_protocol_get_version
241 (session)));
243 /* see the Getting peer's information example */
244 if (debug)
245 print_info (session);
247 gnutls_record_send (session, MSG, strlen (MSG));
249 ret = gnutls_record_recv (session, buffer, MAX_BUF);
250 if (ret == 0)
252 if (debug)
253 success ("client: Peer has closed the TLS connection\n");
254 goto end;
256 else if (ret < 0)
258 fail ("client: Error: %s\n", gnutls_strerror (ret));
259 goto end;
262 if (debug)
264 printf ("- Received %d bytes: ", ret);
265 for (ii = 0; ii < ret; ii++)
267 fputc (buffer[ii], stdout);
269 fputs ("\n", stdout);
272 gnutls_bye (session, GNUTLS_SHUT_RDWR);
274 end:
276 tcp_close (sd);
278 gnutls_deinit (session);
280 gnutls_certificate_free_credentials (xcred);
282 gnutls_global_deinit ();
285 /* This is a sample TLS 1.0 echo server, using X.509 authentication.
288 #define SA struct sockaddr
289 #define MAX_BUF 1024
290 #define PORT 5556 /* listen to 5556 port */
291 #define DH_BITS 1024
293 /* These are global */
294 gnutls_certificate_credentials_t x509_cred;
296 static gnutls_session_t
297 initialize_tls_session (void)
299 gnutls_session_t session;
301 gnutls_init (&session, GNUTLS_SERVER);
303 /* avoid calling all the priority functions, since the defaults
304 * are adequate.
306 gnutls_set_default_priority (session);
308 gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, x509_cred);
310 /* request client certificate if any.
312 gnutls_certificate_server_set_request (session, GNUTLS_CERT_REQUEST);
314 gnutls_dh_set_prime_bits (session, DH_BITS);
316 return session;
319 static gnutls_dh_params_t dh_params;
321 static int
322 generate_dh_params (void)
324 const gnutls_datum_t p3 = { (void *) pkcs3, strlen (pkcs3) };
325 /* Generate Diffie-Hellman parameters - for use with DHE
326 * kx algorithms. These should be discarded and regenerated
327 * once a day, once a week or once a month. Depending on the
328 * security requirements.
330 gnutls_dh_params_init (&dh_params);
331 return gnutls_dh_params_import_pkcs3 (dh_params, &p3, GNUTLS_X509_FMT_PEM);
334 int err, listen_sd, i;
335 int sd, ret;
336 struct sockaddr_in sa_serv;
337 struct sockaddr_in sa_cli;
338 socklen_t client_len;
339 char topbuf[512];
340 gnutls_session_t session;
341 char buffer[MAX_BUF + 1];
342 int optval = 1;
345 static unsigned char server_cert_pem[] =
346 "-----BEGIN CERTIFICATE-----\n"
347 "MIICVjCCAcGgAwIBAgIERiYdMTALBgkqhkiG9w0BAQUwGTEXMBUGA1UEAxMOR251\n"
348 "VExTIHRlc3QgQ0EwHhcNMDcwNDE4MTMyOTIxWhcNMDgwNDE3MTMyOTIxWjA3MRsw\n"
349 "GQYDVQQKExJHbnVUTFMgdGVzdCBzZXJ2ZXIxGDAWBgNVBAMTD3Rlc3QuZ251dGxz\n"
350 "Lm9yZzCBnDALBgkqhkiG9w0BAQEDgYwAMIGIAoGA17pcr6MM8C6pJ1aqU46o63+B\n"
351 "dUxrmL5K6rce+EvDasTaDQC46kwTHzYWk95y78akXrJutsoKiFV1kJbtple8DDt2\n"
352 "DZcevensf9Op7PuFZKBroEjOd35znDET/z3IrqVgbtm2jFqab7a+n2q9p/CgMyf1\n"
353 "tx2S5Zacc1LWn9bIjrECAwEAAaOBkzCBkDAMBgNVHRMBAf8EAjAAMBoGA1UdEQQT\n"
354 "MBGCD3Rlc3QuZ251dGxzLm9yZzATBgNVHSUEDDAKBggrBgEFBQcDATAPBgNVHQ8B\n"
355 "Af8EBQMDB6AAMB0GA1UdDgQWBBTrx0Vu5fglyoyNgw106YbU3VW0dTAfBgNVHSME\n"
356 "GDAWgBTpPBz7rZJu5gakViyi4cBTJ8jylTALBgkqhkiG9w0BAQUDgYEAaFEPTt+7\n"
357 "bzvBuOf7+QmeQcn29kT6Bsyh1RHJXf8KTk5QRfwp6ogbp94JQWcNQ/S7YDFHglD1\n"
358 "AwUNBRXwd3riUsMnsxgeSDxYBfJYbDLeohNBsqaPDJb7XailWbMQKfAbFQ8cnOxg\n"
359 "rOKLUQRWJ0K3HyXRMhbqjdLIaQiCvQLuizo=\n" "-----END CERTIFICATE-----\n";
361 const gnutls_datum_t server_cert = { server_cert_pem,
362 sizeof (server_cert_pem)
365 static unsigned char server_key_pem[] =
366 "-----BEGIN RSA PRIVATE KEY-----\n"
367 "MIICXAIBAAKBgQDXulyvowzwLqknVqpTjqjrf4F1TGuYvkrqtx74S8NqxNoNALjq\n"
368 "TBMfNhaT3nLvxqResm62ygqIVXWQlu2mV7wMO3YNlx696ex/06ns+4VkoGugSM53\n"
369 "fnOcMRP/PciupWBu2baMWppvtr6far2n8KAzJ/W3HZLllpxzUtaf1siOsQIDAQAB\n"
370 "AoGAYAFyKkAYC/PYF8e7+X+tsVCHXppp8AoP8TEZuUqOZz/AArVlle/ROrypg5kl\n"
371 "8YunrvUdzH9R/KZ7saNZlAPLjZyFG9beL/am6Ai7q7Ma5HMqjGU8kTEGwD7K+lbG\n"
372 "iomokKMOl+kkbY/2sI5Czmbm+/PqLXOjtVc5RAsdbgvtmvkCQQDdV5QuU8jap8Hs\n"
373 "Eodv/tLJ2z4+SKCV2k/7FXSKWe0vlrq0cl2qZfoTUYRnKRBcWxc9o92DxK44wgPi\n"
374 "oMQS+O7fAkEA+YG+K9e60sj1K4NYbMPAbYILbZxORDecvP8lcphvwkOVUqbmxOGh\n"
375 "XRmTZUuhBrJhJKKf6u7gf3KWlPl6ShKEbwJASC118cF6nurTjuLf7YKARDjNTEws\n"
376 "qZEeQbdWYINAmCMj0RH2P0mvybrsXSOD5UoDAyO7aWuqkHGcCLv6FGG+qwJAOVqq\n"
377 "tXdUucl6GjOKKw5geIvRRrQMhb/m5scb+5iw8A4LEEHPgGiBaF5NtJZLALgWfo5n\n"
378 "hmC8+G8F0F78znQtPwJBANexu+Tg5KfOnzSILJMo3oXiXhf5PqXIDmbN0BKyCKAQ\n"
379 "LfkcEcUbVfmDaHpvzwY9VEaoMOKVLitETXdNSxVpvWM=\n"
380 "-----END RSA PRIVATE KEY-----\n";
382 const gnutls_datum_t server_key = { server_key_pem,
383 sizeof (server_key_pem)
386 static void
387 server_start (void)
389 /* Socket operations
391 listen_sd = socket (AF_INET, SOCK_STREAM, 0);
392 if (err == -1)
394 perror ("socket");
395 fail ("server: socket failed\n");
396 return;
399 memset (&sa_serv, '\0', sizeof (sa_serv));
400 sa_serv.sin_family = AF_INET;
401 sa_serv.sin_addr.s_addr = INADDR_ANY;
402 sa_serv.sin_port = htons (PORT); /* Server Port number */
404 setsockopt (listen_sd, SOL_SOCKET, SO_REUSEADDR, (void *) &optval,
405 sizeof (int));
407 err = bind (listen_sd, (SA *) & sa_serv, sizeof (sa_serv));
408 if (err == -1)
410 perror ("bind");
411 fail ("server: bind failed\n");
412 return;
415 err = listen (listen_sd, 1024);
416 if (err == -1)
418 perror ("listen");
419 fail ("server: listen failed\n");
420 return;
423 if (debug)
424 success ("server: ready. Listening to port '%d'.\n", PORT);
427 static void
428 server (void)
430 /* this must be called once in the program
432 gnutls_global_init ();
434 gnutls_global_set_log_function (tls_log_func);
435 if (debug)
436 gnutls_global_set_log_level (4711);
438 gnutls_certificate_allocate_credentials (&x509_cred);
439 gnutls_certificate_set_x509_trust_mem (x509_cred, &ca, GNUTLS_X509_FMT_PEM);
441 gnutls_certificate_set_x509_key_mem (x509_cred, &server_cert, &server_key,
442 GNUTLS_X509_FMT_PEM);
444 if (debug)
445 success ("Launched, generating DH parameters...\n");
447 generate_dh_params ();
449 gnutls_certificate_set_dh_params (x509_cred, dh_params);
451 client_len = sizeof (sa_cli);
453 session = initialize_tls_session ();
455 sd = accept (listen_sd, (SA *) & sa_cli, &client_len);
457 if (debug)
458 success ("server: connection from %s, port %d\n",
459 inet_ntop (AF_INET, &sa_cli.sin_addr, topbuf,
460 sizeof (topbuf)), ntohs (sa_cli.sin_port));
462 gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
463 ret = gnutls_handshake (session);
464 if (ret < 0)
466 close (sd);
467 gnutls_deinit (session);
468 fail ("server: Handshake has failed (%s)\n\n", gnutls_strerror (ret));
469 return;
471 if (debug)
472 success ("server: Handshake was completed\n");
474 if (debug)
475 success ("server: TLS version is: %s\n",
476 gnutls_protocol_get_name (gnutls_protocol_get_version
477 (session)));
479 /* see the Getting peer's information example */
480 if (debug)
481 print_info (session);
483 i = 0;
484 for (;;)
486 memset (buffer, 0, MAX_BUF + 1);
487 ret = gnutls_record_recv (session, buffer, MAX_BUF);
489 if (ret == 0)
491 if (debug)
492 success ("server: Peer has closed the GnuTLS connection\n");
493 break;
495 else if (ret < 0)
497 fail ("server: Received corrupted data(%d). Closing...\n", ret);
498 break;
500 else if (ret > 0)
502 /* echo data back to the client
504 gnutls_record_send (session, buffer, strlen (buffer));
507 /* do not wait for the peer to close the connection.
509 gnutls_bye (session, GNUTLS_SHUT_WR);
511 close (sd);
512 gnutls_deinit (session);
514 close (listen_sd);
516 gnutls_certificate_free_credentials (x509_cred);
518 gnutls_dh_params_deinit (dh_params);
520 gnutls_global_deinit ();
522 if (debug)
523 success ("server: finished\n");
527 void
528 doit (void)
530 server_start ();
531 if (error_count)
532 return;
534 child = fork ();
535 if (child < 0)
537 perror ("fork");
538 fail ("fork");
539 return;
542 if (child)
544 int status;
545 /* parent */
546 server ();
547 wait (&status);
549 #if defined WIFEXITED && defined WEXITSTATUS
550 if (WIFEXITED (status) && WEXITSTATUS (status))
552 fail ("server: client failed with exit status %d\n",
553 WEXITSTATUS (status));
555 #endif
557 #if defined WIFSIGNALED && defined WTERMSIG
558 if (WIFSIGNALED (status))
560 fail ("server: client failed with fatal signal %d\n",
561 WTERMSIG (status));
563 #endif
566 else
567 client ();