corrected verification examples
[gnutls.git] / doc / examples / ex-client-resume.c
blob0fe2a8860eaea8d4b7d3ca266a9970420da56baa
1 /* This example code is placed in the public domain. */
3 #ifdef HAVE_CONFIG_H
4 #include <config.h>
5 #endif
7 #include <string.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <gnutls/gnutls.h>
12 /* Those functions are defined in other examples.
14 extern void check_alert (gnutls_session_t session, int ret);
15 extern int tcp_connect (void);
16 extern void tcp_close (int sd);
18 #define MAX_BUF 1024
19 #define CAFILE "/etc/ssl/certs/ca-certificates.crt"
20 #define MSG "GET / HTTP/1.0\r\n\r\n"
22 int
23 main (void)
25 int ret;
26 int sd, ii;
27 gnutls_session_t session;
28 char buffer[MAX_BUF + 1];
29 gnutls_certificate_credentials_t xcred;
31 /* variables used in session resuming
33 int t;
34 char *session_data = NULL;
35 size_t session_data_size = 0;
37 gnutls_global_init ();
39 /* X509 stuff */
40 gnutls_certificate_allocate_credentials (&xcred);
42 gnutls_certificate_set_x509_trust_file (xcred, CAFILE, GNUTLS_X509_FMT_PEM);
44 for (t = 0; t < 2; t++)
45 { /* connect 2 times to the server */
47 sd = tcp_connect ();
49 gnutls_init (&session, GNUTLS_CLIENT);
51 gnutls_priority_set_direct (session, "PERFORMANCE:!ARCFOUR-128", NULL);
53 gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, xcred);
55 if (t > 0)
57 /* if this is not the first time we connect */
58 gnutls_session_set_data (session, session_data, session_data_size);
59 free (session_data);
62 gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
63 gnutls_handshake_set_timeout (session, GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT);
65 /* Perform the TLS handshake
69 ret = gnutls_handshake (session);
71 while (ret < 0 && gnutls_error_is_fatal (ret) == 0);
73 if (ret < 0)
75 fprintf (stderr, "*** Handshake failed\n");
76 gnutls_perror (ret);
77 goto end;
79 else
81 printf ("- Handshake was completed\n");
84 if (t == 0)
85 { /* the first time we connect */
86 /* get the session data size */
87 gnutls_session_get_data (session, NULL, &session_data_size);
88 session_data = malloc (session_data_size);
90 /* put session data to the session variable */
91 gnutls_session_get_data (session, session_data, &session_data_size);
94 else
95 { /* the second time we connect */
97 /* check if we actually resumed the previous session */
98 if (gnutls_session_is_resumed (session) != 0)
100 printf ("- Previous session was resumed\n");
102 else
104 fprintf (stderr, "*** Previous session was NOT resumed\n");
108 /* This function was defined in a previous example
110 /* print_info(session); */
112 gnutls_record_send (session, MSG, strlen (MSG));
114 ret = gnutls_record_recv (session, buffer, MAX_BUF);
115 if (ret == 0)
117 printf ("- Peer has closed the TLS connection\n");
118 goto end;
120 else if (ret < 0)
122 fprintf (stderr, "*** Error: %s\n", gnutls_strerror (ret));
123 goto end;
126 printf ("- Received %d bytes: ", ret);
127 for (ii = 0; ii < ret; ii++)
129 fputc (buffer[ii], stdout);
131 fputs ("\n", stdout);
133 gnutls_bye (session, GNUTLS_SHUT_RDWR);
135 end:
137 tcp_close (sd);
139 gnutls_deinit (session);
141 } /* for() */
143 gnutls_certificate_free_credentials (xcred);
145 gnutls_global_deinit ();
147 return 0;