6 #include <sys/socket.h>
7 #include <netinet/in.h>
12 #define CRLFILE "crl.pem"
13 #define CAFILE "ca.pem"
14 #define SA struct sockaddr
15 #define MSG "GET / HTTP/
1.0\r\n\r\n"
19 const char *PORT = "
443";
20 const char *SERVER = "
127.0.0.1";
23 struct sockaddr_in sa;
25 char buffer
[MAX_BUF +
1];
26 GNUTLS_CERTIFICATE_CLIENT_CREDENTIALS xcred;
27 const int protocol_priority
[] =
{ GNUTLS_TLS1, GNUTLS_SSL3,
0 };
28 const int kx_priority
[] =
{ GNUTLS_KX_RSA,
0 };
29 const int cipher_priority
[] =
{ GNUTLS_CIPHER_3DES_CBC, GNUTLS_CIPHER_ARCFOUR,
0};
30 const int comp_priority
[] =
{ GNUTLS_COMP_ZLIB, GNUTLS_COMP_NULL,
0 };
31 const int mac_priority
[] =
{ GNUTLS_MAC_SHA, GNUTLS_MAC_MD5,
0 };
34 if (gnutls_global_init() <
0)
{
35 fprintf(stderr, "global state initialization error
\n");
39 if (gnutls_certificate_allocate_client_sc(&xcred) <
0)
{
40 fprintf(stderr, "memory error
\n");
43 /* set's the trusted cas file
45 gnutls_certificate_set_x509_trust_file(xcred, CAFILE, CRLFILE, GNUTLS_X509_FMT_PEM);
49 sd = socket(AF_INET, SOCK_STREAM,
0);
51 memset(&sa, '
\0', sizeof(sa));
52 sa.sin_family = AF_INET;
53 sa.sin_port = htons(atoi(PORT));
54 inet_pton(AF_INET, SERVER, &sa.sin_addr);
56 err = connect(sd, (SA *) & sa, sizeof(sa));
58 fprintf(stderr, "Connect error
\n");
61 /* Initialize TLS state
63 gnutls_init(&state, GNUTLS_CLIENT);
65 /* allow both SSL3 and TLS1
67 gnutls_protocol_set_priority(state, protocol_priority);
69 /* allow only ARCFOUR and
3DES ciphers
70 * (
3DES has the highest priority)
72 gnutls_cipher_set_priority(state, cipher_priority);
74 /* only allow null compression
76 gnutls_compression_set_priority(state, comp_priority);
80 gnutls_kx_set_priority(state, kx_priority);
82 /* allow the usage of both SHA and MD5
84 gnutls_mac_set_priority(state, mac_priority);
87 /* put the x509 credentials to the current state
89 gnutls_cred_set(state, GNUTLS_CRD_CERTIFICATE, xcred);
92 gnutls_transport_set_ptr( state, sd);
93 /* Perform the TLS handshake
95 ret = gnutls_handshake( state);
98 fprintf(stderr, "*** Handshake failed
\n");
102 printf("- Handshake was completed
\n");
105 gnutls_record_send( state, MSG, strlen(MSG));
107 ret = gnutls_record_recv( state, buffer, MAX_BUF);
108 if (gnutls_error_is_fatal(ret) ==
1 || ret ==
0)
{
110 printf("- Peer has closed the GNUTLS connection
\n");
113 fprintf(stderr, "*** Received corrupted data(
%d) - server has terminated the connection abnormally\n",
118 if (ret == GNUTLS_E_WARNING_ALERT_RECEIVED || ret == GNUTLS_E_FATAL_ALERT_RECEIVED)
119 printf("* Received alert
[%d]\n", gnutls_alert_get(state));
120 if (ret == GNUTLS_E_REHANDSHAKE)
121 printf("* Received HelloRequest message (server asked to rehandshake)
\n");
122 gnutls_alert_send_appropriate( state, ret); /* we don't want rehandshake */
126 printf("- Received
%d bytes: ", ret);
127 for (ii =
0; ii < ret; ii++)
{
128 fputc(buffer
[ii
], stdout);
132 gnutls_bye( state, GNUTLS_SHUT_RDWR);
136 shutdown(sd, SHUT_RDWR); /* no more receptions */
139 gnutls_deinit(state);
141 gnutls_certificate_free_client_sc(xcred);
143 gnutls_global_deinit();