Added gnutls_x509_crl_reason_flags_t.
[gnutls.git] / doc / examples / ex-client-srp.c
blob112e9f0476404725933046273ae1806651de1a90
1 /* This example code is placed in the public domain. */
3 #ifdef HAVE_CONFIG_H
4 #include <config.h>
5 #endif
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.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 USERNAME "user"
20 #define PASSWORD "pass"
21 #define CAFILE "/etc/ssl/certs/ca-certificates.crt"
22 #define MSG "GET / HTTP/1.0\r\n\r\n"
24 int
25 main (void)
27 int ret;
28 int sd, ii;
29 gnutls_session_t session;
30 char buffer[MAX_BUF + 1];
31 gnutls_srp_client_credentials_t srp_cred;
32 gnutls_certificate_credentials_t cert_cred;
34 gnutls_global_init ();
36 gnutls_srp_allocate_client_credentials (&srp_cred);
37 gnutls_certificate_allocate_credentials (&cert_cred);
39 gnutls_certificate_set_x509_trust_file (cert_cred, CAFILE,
40 GNUTLS_X509_FMT_PEM);
41 gnutls_srp_set_client_credentials (srp_cred, USERNAME, PASSWORD);
43 /* connects to server
45 sd = tcp_connect ();
47 /* Initialize TLS session
49 gnutls_init (&session, GNUTLS_CLIENT);
52 /* Set the priorities.
54 gnutls_priority_set_direct (session, "NORMAL:+SRP:+SRP-RSA:+SRP-DSS", NULL);
56 /* put the SRP credentials to the current session
58 gnutls_credentials_set (session, GNUTLS_CRD_SRP, srp_cred);
59 gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, cert_cred);
61 gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
62 gnutls_handshake_set_timeout (session, GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT);
64 /* Perform the TLS handshake
68 ret = gnutls_handshake (session);
70 while (ret < 0 && gnutls_error_is_fatal (ret) == 0);
72 if (ret < 0)
74 fprintf (stderr, "*** Handshake failed\n");
75 gnutls_perror (ret);
76 goto end;
78 else
80 printf ("- Handshake was completed\n");
83 gnutls_record_send (session, MSG, strlen (MSG));
85 ret = gnutls_record_recv (session, buffer, MAX_BUF);
86 if (gnutls_error_is_fatal (ret) != 0 || ret == 0)
88 if (ret == 0)
90 printf ("- Peer has closed the GnuTLS connection\n");
91 goto end;
93 else
95 fprintf (stderr, "*** Error: %s\n", gnutls_strerror (ret));
96 goto end;
99 else
100 check_alert (session, ret);
102 if (ret > 0)
104 printf ("- Received %d bytes: ", ret);
105 for (ii = 0; ii < ret; ii++)
107 fputc (buffer[ii], stdout);
109 fputs ("\n", stdout);
111 gnutls_bye (session, GNUTLS_SHUT_RDWR);
113 end:
115 tcp_close (sd);
117 gnutls_deinit (session);
119 gnutls_srp_free_client_credentials (srp_cred);
120 gnutls_certificate_free_credentials (cert_cred);
122 gnutls_global_deinit ();
124 return 0;