1 /* This example code is placed in the public domain. */
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
);
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"
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
,
41 gnutls_srp_set_client_credentials (srp_cred
, USERNAME
, PASSWORD
);
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);
74 fprintf (stderr
, "*** Handshake failed\n");
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)
90 printf ("- Peer has closed the GnuTLS connection\n");
95 fprintf (stderr
, "*** Error: %s\n", gnutls_strerror (ret
));
100 check_alert (session
, ret
);
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
);
117 gnutls_deinit (session
);
119 gnutls_srp_free_client_credentials (srp_cred
);
120 gnutls_certificate_free_credentials (cert_cred
);
122 gnutls_global_deinit ();