|
Packit Service |
4684c1 |
/* This example code is placed in the public domain. */
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#ifdef HAVE_CONFIG_H
|
|
Packit Service |
4684c1 |
#include <config.h>
|
|
Packit Service |
4684c1 |
#endif
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#include <string.h>
|
|
Packit Service |
4684c1 |
#include <stdio.h>
|
|
Packit Service |
4684c1 |
#include <stdlib.h>
|
|
Packit Service |
4684c1 |
#include <assert.h>
|
|
Packit Service |
4684c1 |
#include <gnutls/gnutls.h>
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
extern void check_alert(gnutls_session_t session, int ret);
|
|
Packit Service |
4684c1 |
extern int tcp_connect(void);
|
|
Packit Service |
4684c1 |
extern void tcp_close(int sd);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* A very basic TLS client, with X.509 authentication and server certificate
|
|
Packit Service |
4684c1 |
* verification as well as session resumption.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Note that error recovery is minimal for simplicity.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#define CHECK(x) assert((x)>=0)
|
|
Packit Service |
4684c1 |
#define LOOP_CHECK(rval, cmd) \
|
|
Packit Service |
4684c1 |
do { \
|
|
Packit Service |
4684c1 |
rval = cmd; \
|
|
Packit Service |
4684c1 |
} while(rval == GNUTLS_E_AGAIN || rval == GNUTLS_E_INTERRUPTED); \
|
|
Packit Service |
4684c1 |
assert(rval >= 0)
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#define MAX_BUF 1024
|
|
Packit Service |
4684c1 |
#define MSG "GET / HTTP/1.0\r\n\r\n"
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
int main(void)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
int ret;
|
|
Packit Service |
4684c1 |
int sd, ii;
|
|
Packit Service |
4684c1 |
gnutls_session_t session;
|
|
Packit Service |
4684c1 |
char buffer[MAX_BUF + 1];
|
|
Packit Service |
4684c1 |
gnutls_certificate_credentials_t xcred;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* variables used in session resuming
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
int t;
|
|
Packit Service |
4684c1 |
gnutls_datum_t sdata;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* for backwards compatibility with gnutls < 3.3.0 */
|
|
Packit Service |
4684c1 |
CHECK(gnutls_global_init());
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
CHECK(gnutls_certificate_allocate_credentials(&xcred));
|
|
Packit Service |
4684c1 |
CHECK(gnutls_certificate_set_x509_system_trust(xcred));
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
for (t = 0; t < 2; t++) { /* connect 2 times to the server */
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
sd = tcp_connect();
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
CHECK(gnutls_init(&session, GNUTLS_CLIENT));
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
CHECK(gnutls_server_name_set(session, GNUTLS_NAME_DNS,
|
|
Packit Service |
4684c1 |
"www.example.com",
|
|
Packit Service |
4684c1 |
strlen("www.example.com")));
|
|
Packit Service |
4684c1 |
gnutls_session_set_verify_cert(session, "www.example.com", 0);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
CHECK(gnutls_set_default_priority(session));
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_transport_set_int(session, sd);
|
|
Packit Service |
4684c1 |
gnutls_handshake_set_timeout(session,
|
|
Packit Service |
4684c1 |
GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE,
|
|
Packit Service |
4684c1 |
xcred);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (t > 0) {
|
|
Packit Service |
4684c1 |
/* if this is not the first time we connect */
|
|
Packit Service |
4684c1 |
CHECK(gnutls_session_set_data(session, sdata.data,
|
|
Packit Service |
4684c1 |
sdata.size));
|
|
Packit Service |
4684c1 |
gnutls_free(sdata.data);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* Perform the TLS handshake
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
do {
|
|
Packit Service |
4684c1 |
ret = gnutls_handshake(session);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (ret < 0) {
|
|
Packit Service |
4684c1 |
fprintf(stderr, "*** Handshake failed\n");
|
|
Packit Service |
4684c1 |
gnutls_perror(ret);
|
|
Packit Service |
4684c1 |
goto end;
|
|
Packit Service |
4684c1 |
} else {
|
|
Packit Service |
4684c1 |
printf("- Handshake was completed\n");
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (t == 0) { /* the first time we connect */
|
|
Packit Service |
4684c1 |
/* get the session data */
|
|
Packit Service |
4684c1 |
CHECK(gnutls_session_get_data2(session, &sdata));
|
|
Packit Service |
4684c1 |
} else { /* the second time we connect */
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* check if we actually resumed the previous session */
|
|
Packit Service |
4684c1 |
if (gnutls_session_is_resumed(session) != 0) {
|
|
Packit Service |
4684c1 |
printf("- Previous session was resumed\n");
|
|
Packit Service |
4684c1 |
} else {
|
|
Packit Service |
4684c1 |
fprintf(stderr,
|
|
Packit Service |
4684c1 |
"*** Previous session was NOT resumed\n");
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
LOOP_CHECK(ret, gnutls_record_send(session, MSG, strlen(MSG)));
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
LOOP_CHECK(ret, gnutls_record_recv(session, buffer, MAX_BUF));
|
|
Packit Service |
4684c1 |
if (ret == 0) {
|
|
Packit Service |
4684c1 |
printf("- Peer has closed the TLS connection\n");
|
|
Packit Service |
4684c1 |
goto end;
|
|
Packit Service |
4684c1 |
} else if (ret < 0 && gnutls_error_is_fatal(ret) == 0) {
|
|
Packit Service |
4684c1 |
fprintf(stderr, "*** Warning: %s\n",
|
|
Packit Service |
4684c1 |
gnutls_strerror(ret));
|
|
Packit Service |
4684c1 |
} else if (ret < 0) {
|
|
Packit Service |
4684c1 |
fprintf(stderr, "*** Error: %s\n",
|
|
Packit Service |
4684c1 |
gnutls_strerror(ret));
|
|
Packit Service |
4684c1 |
goto end;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (ret > 0) {
|
|
Packit Service |
4684c1 |
printf("- Received %d bytes: ", ret);
|
|
Packit Service |
4684c1 |
for (ii = 0; ii < ret; ii++) {
|
|
Packit Service |
4684c1 |
fputc(buffer[ii], stdout);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
fputs("\n", stdout);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_bye(session, GNUTLS_SHUT_RDWR);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
end:
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
tcp_close(sd);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_deinit(session);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
} /* for() */
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_certificate_free_credentials(xcred);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_global_deinit();
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return 0;
|
|
Packit Service |
4684c1 |
}
|