1 /* This example code is placed in the public domain. */
10 #include <sys/types.h>
11 #include <sys/socket.h>
12 #include <arpa/inet.h>
14 #include <gnutls/gnutls.h>
16 /* A very basic TLS client, with anonymous authentication.
20 #define MSG "GET / HTTP/1.0\r\n\r\n"
22 extern int tcp_connect (void);
23 extern void tcp_close (int sd
);
29 gnutls_session_t session
;
30 char buffer
[MAX_BUF
+ 1];
31 gnutls_anon_client_credentials_t anoncred
;
32 /* Need to enable anonymous KX specifically. */
34 gnutls_global_init ();
36 gnutls_anon_allocate_client_credentials (&anoncred
);
38 /* Initialize TLS session
40 gnutls_init (&session
, GNUTLS_CLIENT
);
42 /* Use default priorities */
43 gnutls_priority_set_direct (session
, "PERFORMANCE:+ANON-ECDH:+ANON-DH",
46 /* put the anonymous credentials to the current session
48 gnutls_credentials_set (session
, GNUTLS_CRD_ANON
, anoncred
);
50 /* connect to the peer
54 gnutls_transport_set_ptr (session
, (gnutls_transport_ptr_t
) sd
);
55 gnutls_handshake_set_timeout (session
, GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT
);
57 /* Perform the TLS handshake
61 ret
= gnutls_handshake (session
);
63 while (ret
< 0 && gnutls_error_is_fatal (ret
) == 0);
67 fprintf (stderr
, "*** Handshake failed\n");
73 printf ("- Handshake was completed\n");
76 gnutls_record_send (session
, MSG
, strlen (MSG
));
78 ret
= gnutls_record_recv (session
, buffer
, MAX_BUF
);
81 printf ("- Peer has closed the TLS connection\n");
86 fprintf (stderr
, "*** Error: %s\n", gnutls_strerror (ret
));
90 printf ("- Received %d bytes: ", ret
);
91 for (ii
= 0; ii
< ret
; ii
++)
93 fputc (buffer
[ii
], stdout
);
97 gnutls_bye (session
, GNUTLS_SHUT_RDWR
);
103 gnutls_deinit (session
);
105 gnutls_anon_free_client_credentials (anoncred
);
107 gnutls_global_deinit ();