Improved PKCS7 support
[gnutls.git] / doc / tex / ex2.tex
blobff83ed8078d4d5c4edca9f1b470d7d492d47f08d
1 \begin{verbatim}
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <sys/types.h>
6 #include <sys/socket.h>
7 #include <netinet/in.h>
8 #include <arpa/inet.h>
9 #include <gnutls.h>
11 #define MAX_BUF 1024
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"
17 int main()
19 const char *PORT = "443";
20 const char *SERVER = "127.0.0.1";
21 int err, ret;
22 int sd, ii;
23 struct sockaddr_in sa;
24 GNUTLS_STATE state;
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");
36 exit(1);
38 /* X509 stuff */
39 if (gnutls_certificate_allocate_client_sc(&xcred) < 0) {
40 fprintf(stderr, "memory error\n");
41 exit(1);
43 /* set's the trusted cas file
45 gnutls_certificate_set_x509_trust_file(xcred, CAFILE, CRLFILE, GNUTLS_X509_FMT_PEM);
47 /* connects to server
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));
57 if (err < 0) {
58 fprintf(stderr, "Connect error\n");
59 exit(1);
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);
78 /* use GNUTLS_KX_RSA
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);
97 if (ret < 0) {
98 fprintf(stderr, "*** Handshake failed\n");
99 gnutls_perror(ret);
100 goto end;
101 } else {
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) {
109 if (ret == 0) {
110 printf("- Peer has closed the GNUTLS connection\n");
111 goto end;
112 } else {
113 fprintf(stderr, "*** Received corrupted data(%d) - server has terminated the connection abnormally\n",
114 ret);
115 goto end;
117 } else {
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 */
125 if (ret > 0) {
126 printf("- Received %d bytes: ", ret);
127 for (ii = 0; ii < ret; ii++) {
128 fputc(buffer[ii], stdout);
130 fputs("\n", stdout);
132 gnutls_bye( state, GNUTLS_SHUT_RDWR);
134 end:
136 shutdown(sd, SHUT_RDWR); /* no more receptions */
137 close(sd);
139 gnutls_deinit(state);
141 gnutls_certificate_free_client_sc(xcred);
143 gnutls_global_deinit();
145 return 0;
148 \end{verbatim}